Is the Promise constructor synchronous? [duplicate]

烈酒焚心 提交于 2020-06-27 16:25:47

问题


Say the code in a Promise constructor contains synchronous assignments. I have something like this:

function x() {
    let rejector = null;
    new Promise((resolve, reject) => {
        rejector = reject;
        // async code follows - setTimeout, network requests, etc.
    });
    return rejector;
}

Is the synchronous code in the Promise constructor guarunteed to execute, in this example, before the return statement of its containing function?

For me thus far it works every time — typeof x() == 'function' is always true; but I feel like I just happen to be winning a race. I ran it through a loop, 10^8 times, checking to see if I'd ever 'lose the race', but no, it was always successful. Does this always hold?

来源:https://stackoverflow.com/questions/59415313/is-the-promise-constructor-synchronous

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