The Promise. resolve() method "resolves" a given value to a Promise . If the value is a promise, that promise is returned; if the value is a thenable, Promise. resolve() will call the then() method with two callbacks it prepared; otherwise the returned promise will be fulfilled with the value.
Promise resolve() method:If the value is a promise then promise is returned. If the value has a “then” attached to the promise, then the returned promise will follow that “then” to till the final state. The promise fulfilled with its value will be returned.
Promises have an API which encourages casually dangerous code. Promises lack a convenient API to safely work with data. Promises co-mingle rejected promises and unintended runtime exceptions.
The Promise. all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises.
JavaScript has the concept of "run to completion". Unless an error is thrown, a function is executed until a return
statement or its end is reached. Other code outside of the function can't interfere with that (unless, again, an error is thrown).
If you want resolve()
to exit your initializer function, you have to prepend it by return
:
return new Promise(function(resolve, reject) {
return resolve();
console.log("Not doing more stuff after a return statement");
});
The callbacks that will be invoked when you resolve
a promise are still required by the specification to be called asynchronously. This is to ensure consistent behaviour when using promises for a mix of synchronous and asynchronous actions.
Therefore when you invoke resolve
the callback is queued, and function execution continues immediately with any code following the resolve()
call.
Only once the JS event loop is given back control can the callback be removed from the queue and actually invoked.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With