Suhaib
Suhaib

Reputation: 2061

JS promise is getting called without calling it

I don't know why is promise1 keeps getting called even though I never tried to resolve it.

                    function successCallback() {
                        console.log("doSomething func succeeded with sucess");
                    }
                    
                    function failureCallback() {
                        console.log("doSomething func failed with error");
                    }

                    let promis1 = new Promise((res, rej) => {
                    setTimeout(() => {
                        console.log(`Finally got called`);
                        return res(successCallback());
                    }, 5000);
                    });
    
                    function promise2(value) {
                    return new Promise((res, rej) => {
                        console.log(`This is getting called for some reason ${value}`)
                        return res(failureCallback());
                    });
                    }

                    Promise.resolve("6").then(promise2(6));

And here is the output that Im getting:

This is getting called for some reason 6

doSomething func failed with error

Finally got called

doSomething func succeeded with sucess

[Done] exited with code=0 in 5.525 seconds

Upvotes: 0

Views: 114

Answers (3)

Jaromanda X
Jaromanda X

Reputation: 1

Perhaps this will show you the flow of your code

setTimeout(() => console.log(9)); // 9 will log once all the "synchronous" code below is completed
console.log(1);
let promis1 = new Promise((res, rej) => {
  console.log(2);
  setTimeout(() => {
    console.log(10);
    return res('resolved 1');
  }, 5000);
});
console.log(3);
function promise2(value) {
  console.log(5);
  return new Promise((res, rej) => {
    console.log(6);
    return res('resolved 2');
  });
}
console.log(4);
promise2().then(() => {
  console.log(8);
});
console.log(7);

note: some (most?) promise implementations (including bluebird) will output 1,2,3,4,5,6,7,9,8,10 - because .then callback is added to the end of the message queue - whereas, it may be the case that in native promises, the .then callback jumps the queue! (or maybe there's more to javascript engines these days than this simple model

Upvotes: 2

HMR
HMR

Reputation: 39320

The function passed to new Promise is called immediately and synchronously by the promise constructor (unlike setTimeout) and is on the same stack as new Promise.

If the function passed to it throws then it results in a rejected promise:

console.log(1);
const p = new Promise(
  (res,rej)=>{
    console.log(2);
    res();
  }
);
p.then(
  ()=>console.log(7)
);
console.log(3);

console.log(4);
const p2 = new Promise(
  (res,rej)=>{
    console.log(5);
    throw("an error");
  }
);
p2.catch(
  ()=>console.log(8)
);
console.log(6);

Upvotes: 2

Abiud Orina
Abiud Orina

Reputation: 1232

If this is part of a larger program. Change let promis1 = new Promise((res, rej) => { to let promise1 = new Promise((res, rej) => {

Hint : Spelling of the word "promise"

Upvotes: -2

Related Questions