Waterfr Villa
Waterfr Villa

Reputation: 1317

diffrence between return a resolved promise and regular promise which only handles resolve

what is the difference between the followings: In my opinion they both return a resolved promise but I think it might not be correct. I was wondering if they is a difference ?

getQuery =  function () {
   return Promise.resolve({
        name: "some query name";
   });
}

executeQuery = function () {
    return new Promise((resolve, reject) => {
           resolve(mockQueryResult);
    }); },

Upvotes: 1

Views: 40

Answers (2)

p.s.w.g
p.s.w.g

Reputation: 149020

Looking at the ECMAScript 2015 specification (emphasis mine):

The resolve function returns either a new promise resolved with the passed argument, or the argument itself if the argument is a promise produced by this constructor.

Then reading further down in the implementation:

6. Let resolveResult be Call(promiseCapability.[[Resolve]], undefined, «x»).

I believe this means that according to the spec, the results of the two are identical. Of course, if you're using a third-party library or polyfill, it's possible that you might get different results.

Upvotes: 1

Kolzar
Kolzar

Reputation: 873

IMHO, the first function recall the resolve method of the class Promise. The second function returns and instantiates a promise object, and this function becomes an async func and if it goes well then recall the resolve.

 getQuery =  function () {
   Promise.resolve({
    name: "some query name";
   });
 }

executeQuery = function () {
   return new Promise((resolve, reject) => {
     resolve(mockQueryResult);
}); },

Upvotes: 0

Related Questions