Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Promise.resolve vs new Promise(resolve)

People also ask

Can a promise resolve another promise?

"The point is that a promise will never resolve to another promise. It will always resolve to that promise's (eventual) value." Is jQuery $.

What is promise resolve () then?

resolve() 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.

Is promise resolve same as return?

resolve("aaa") is the same as return Promise. resolve(Promise. resolve("aaa")) - since resolve is idempotent calling it on a value more than once has the same result.

Why do we use promise resolve?

The only reason to use Promise. resolve is converting a value that might be a promise to a promise. Promise. resolve(value) is a convenience method that does new Promise(resolve => resolve(value) .


Contrary to both answers in the comments - there is a difference.

While

Promise.resolve(x);

is basically the same as

new Promise(function(r){ r(x); });

there is a subtlety.

Promise returning functions should generally have the guarantee that they should not throw synchronously since they might throw asynchronously. In order to prevent unexpected results and race conditions - throws are usually converted to returned rejections.

With this in mind - when the spec was created the promise constructor is throw safe.

What if someObject is undefined?

  • Way A returns a rejected promise.
  • Way B throws synchronously.

Bluebird saw this, and Petka added Promise.method to address this issue so you can keep using return values. So the correct and easiest way to write this in Bluebird is actually neither - it is:

var someFunction = Promise.method(function someFunction(someObject){
    someObject.resolved = true;
    return someObject;
});

Promise.method will convert throws to rejects and returns to resolves for you. It is the most throw safe way to do this and it assimilatesthenables through return values so it'd work even if someObject is in fact a promise itself.

In general, Promise.resolve is used for casting objects and foreign promises (thenables) to promises. That's its use case.


There is another difference not mentioned by the above answers or comments:

If someObject is a Promise, new Promise(resolve) would cost two additional tick.


Compare two following code snippet:

const p = new Promise(resovle => setTimeout(resovle));

new Promise(resolve => resolve(p)).then(() => {
  console.log("tick 3");
});

p.then(() => {
  console.log("tick 1");
}).then(() => {
  console.log("tick 2");
});

const p = new Promise(resolve => setTimeout(resolve));

Promise.resolve(p).then(() => {
  console.log("tick 3");
});

p.then(() => {
  console.log("tick 1");
}).then(() => {
  console.log("tick 2");
});

The second snippet would print 'tick 3' firstly. Why?

  • If the value is a promise, Promise.resolve(value) would return value exactly. Promise.resolve(value) === value would be true. see MDN

  • But new Promise(resolve => resolve(value)) would return a new promise which has locked in to follow the value promise. It needs an extra one tick to make the 'locking-in'.

      // something like:
      addToMicroTaskQueue(() => {
        p.then(() => {
          /* resolve newly promise */
        })
          // all subsequent .then on newly promise go on from here
          .then(() => {
            console.log("tick 3");
          });
      });
    

    The tick 1 .then call would run first.


References:
  • http://exploringjs.com/es6/ch_promises.html#sec_demo-promise