4

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?

DWR
  • 888
  • 1
  • 8
  • 15

0 Answers0