Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the Promise constructor callback executed asynchronously?

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.

like image 572
qnimate Avatar asked Apr 30 '15 08:04

qnimate


People also ask

Are promises executed asynchronously?

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.

Can promise constructor be async?

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.

Is promise all synchronous or asynchronous?

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.

Are promises always asynchronous?

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.


1 Answers

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)

  1. Let completion be Call(executor, undefined, «resolvingFunctions.[[Resolve]], resolvingFunctions.[[Reject]]»).
  2. If completion is an abrupt completion, then
    • Let status be Call(resolvingFunctions.[[Reject]], undefined, «completion.[[value]]»).
    • ReturnIfAbrupt(status).

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.

PerformPromiseThen

  1. Else if the value of promise's [[PromiseState]] internal slot is "fulfilled",
    • Let value be the value of promise's [[PromiseResult]] internal slot.
    • Perform EnqueueJob("PromiseJobs", PromiseReactionJob, «fulfillReaction, value»).
  2. Else if the value of promise's [[PromiseState]] internal slot is "rejected",
    • Let reason be the value of promise's [[PromiseResult]] internal slot.
    • Perform EnqueueJob("PromiseJobs", PromiseReactionJob, «rejectReaction, reason»).

TLDR: the function passed to the promise is executed synchronously, but subsequent then calls are always executed asynchronously.

like image 97
Dan Avatar answered Sep 30 '22 19:09

Dan