Return value from a Promise constructor

The function passed to Promise isn’t a callback for onFulfilled or onRejected. MDN calls it the executor. Think of it as the async context that the promise is attempting to capture. Returning from an async method doesn’t work (or make sense), hence you have to call resolve or reject. For example

var returnVal = new Promise(function() {
     return setTimeout(function() {
         return 27;
     });
});

… does not work as intended. If you were to return a value from the executor before your async calls finished, the promise couldn’t be re-resolved.

Also, it could be ambigous with the implicit return undefined; at the end of the function. Consider these executors that function the same way.

// A
function a() { return undefined; }

// B
function b() { }

What would tell the Promise constructor that you really wanted to resolve with undefined?

a() === b(); // true

Leave a Comment