How to obtain username from Promise?

I am using Discord.js and I have no idea how to get the value out of a Promise. Here is some context:

///File: commands/mail.js///
module.exports = {

        name: 'mail',
        description: 'A system of collecting, sending, and receiving mail',
        async execute(client,message, args) {
                const fs = require('fs');
        ...
                ...
                        switch(args[0]){
                        ...
                        case "check":
                            ...
                            var name;
                            var val;
/*  [object Promise]-->  */ var out = client.users.fetch(recipient.senders[i]);
                            client.users.fetch(recipient.senders[i]).then((name)=>{
                                console.log("name.username(inside): "+name.username);
                                val = name.username;
                                return (name);
                            });
                            console.log("name(outside): ",name);
                            console.log("out: ",out);
                            console.log("out.username: ",out.username);
                            console.log("val: ",val);
                            
                            /* next line will get an error saying name.username is undefined */
                            //console.log("name.username(outside): "+name.username); 
                            ...

and I get the output:

name(outside): undefined
out:  Promise {
  User {
    id: '1456051390970320640',
    bot: false,
    username: 'Sir_Ross',
    discriminator: '3871',
    avatar: 'a956037b05108e4971b93f3ffde97b6c',
    lastMessageID: '739552900342349856',
    lastMessageChannelID: '739150130339340342',
    flags: UserFlags { bitfield: 0 }
  }
}
val: undefined
out.username: undefined
name.username(inside): Sir_Ross

My issue is that I can obviously get my username and print it to console, but I can’t get it out of the Promise. How do I get the value out of the Promise so that I can use it elsewhere?

Full Code:

module.exports = {

    name: 'mail',
    description: 'A system of collecting, sending, and receiving mail',
    async execute(client,message, args) {
        const fs = require('fs');
        var path;
        if(!args.length){
            //No Arguments

            return 0;
        }
        
        switch(args[0]){
            case "send":
                // Send Mail
                if(args.length<3){
                    message.reply('Invalid Arguments: !mail send <recipient_id> <message>');
                }else{
                    letter = "";
                    spaces = 0;
                    for(i = 0; i<message.content.length;i++){
                        if(spaces<3){
                            if(message.content[i]===' '){
                                spaces++;
                            }
                            continue;
                        }else{
                            letter+=message.content[i];
                        }
                    }
                    
                    const fs = require('fs');
                    path = 'mail/';

Leave a Comment