Suppose I have this code
function y(resolve, reject)
{
console.log("Result");
resolve();
}
var promise = new Promise(y);
What I want to know is whether the function y
will be executed asynchronously or not.
A promise is used to handle the asynchronous result of an operation. JavaScript is designed to not wait for an asynchronous block of code to completely execute before other synchronous parts of the code can run. With Promises, we can defer the execution of a code block until an async request is completed.
You can create a promise using the promise constructor like this: let promise = new Promise(function(resolve, reject) { // Make an asynchronous call and either resolve or reject }); In most cases, a promise may be used for an asynchronous operation.
Fulfillment. The returned promise is fulfilled with an array containing all the fulfilled values (including non-promise values) in the iterable passed as the argument. If an empty iterable is passed, then the promise returned by this method is fulfilled synchronously.
In testing I've found that JavaScript Promises are always asynchronous regardless of whether or not they contain any asynchronous functions in their chain. Here is some code that shows the order of operations in console.
It depends on the implementation of the promise. If we check the spec. You can find the final spec here - since this answer was originally written, it has been finalized.
Here is the relevant excerpt (you can find the original source here)
The ES6 standard indicates that the fulfillment of a promise is always asynchronous (See section 25.4.5.3, Promise.prototype.then
and accompanying section 25.4.5.3.1, PerformPromiseThen
). I have placed the relevant material below.
TLDR: the function passed to the promise is executed synchronously, but subsequent then
calls are always executed asynchronously.
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