JS promise is getting called without calling it

我的梦境 提交于 2019-12-11 17:36:56

问题


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


回答1:


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);



回答2:


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




回答3:


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"



来源:https://stackoverflow.com/questions/48919014/js-promise-is-getting-called-without-calling-it

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!