2

I found some code snippet that has the same behavior, But the different syntax of resolving a promise from a function :

return new Promise(resolve => resolve(fruits[name]))

and

return Promise.resolve(fruits[name])

What is the difference between both?

Arushi Bajpai
  • 91
  • 3
  • 6
  • 1
    Second one is shorter. – Ry- Jan 27 '19 at 17:33
  • 1
    second one returns a promise resolved with a value provided as input of the `.resolve` method. First one accepts a callback which can do any operations inside, and then `reject` or `resolve` depending on those operations – quirimmo Jan 27 '19 at 17:36
  • 1
    Notice that `Promise.resolve` will return the argument if it already is a promise - although this should never matter in practice, it's a subtle difference. – Bergi Jan 27 '19 at 17:55
  • @quirimmo Thanks. I got the point. – Arushi Bajpai Jan 28 '19 at 10:30

0 Answers0