Smitk
Smitk

Reputation: 91

execute .then() in a return promise

My code is:-

function scammer(message) {
    return new Promise((resolve,reject)=>{
        if(condition){
            if (condition) {
                bot.KickMember(params).then((message)=>{
                    console.log("kicked");
                });
            } else {
                console.log("Not kicked");
            }
            resolve();
       }else{
          reject();
       }
   });
}

In the above code, function scammer works perfectly also the if-else statement but the then() does not work since I put it into a new promise, is there a way to execute then() without getting it out of the new promise.

Upvotes: 1

Views: 277

Answers (2)

Hardik Shah
Hardik Shah

Reputation: 4200

Probably like this: resolve() should inside the bot promise.

function scammer(message) {
    return new Promise((resolve,reject)=>{
        if (condition) {
            bot.KickMember(params).then((message)=>{
                console.log("kicked");
                resolve();
            });
        } else {
            console.log("Not kicked");
            reject();
        }
    });
}

As per your code resolve() will execute before you land inside then of bot promise.

Correct me if I am wrong.

Upvotes: 0

Jonas Wilms
Jonas Wilms

Reputation: 138247

You don't need to create a promise, just return the one you got:

if (condition) {
    return bot.KickMember(params).then((message) => {
        console.log("kicked");
    });
} else {
    console.log("Not kicked");
    return Promise.reject(new Error("some reason"));
}

Upvotes: 3

Related Questions