4

Consider these two functions:

function a(){
  //...
  if(something) {
    return Promise.resolve();
  } else {
    return Promise.reject();
  }
}  

function b(){
  //...
  return new Promise((resolve, reject) => {
    if(something) {
      resolve();
    } else {
      reject();
    }
  });
}  

I met more often with second approach, but first one looks a bit cleaner for me. Are there any specific, rare use cases when code would work different with each approach, or is it just semantics?

Tomasz Nowak
  • 175
  • 2
  • 10
  • If you don't have logic inside `new Promise(` which can throw, and it's only `if` inside, then both versions are equivalent. – dfsq Apr 23 '18 at 11:50
  • completely the same – Hyyan Abo Fakher Apr 23 '18 at 11:50
  • @dfsq, I am not sure but does first function creating Two objects but later one creates only one . So second function is saving memory. Please clear it. Thanks! – Vikasdeep Singh Apr 23 '18 at 11:51
  • Possible duplicate of [What's the difference between returning value or Promise.resolve from then()](https://stackoverflow.com/questions/27715275/whats-the-difference-between-returning-value-or-promise-resolve-from-then#27716518) – Amthieu Apr 23 '18 at 11:51

2 Answers2

3

Both examples are pointless because code is synchronous.

If you have a traditional callback function such as setTimeout you have to use new Promise to convert it to a promise (you cannot return Promise.resolve(value) from a callback:

const later = (howLong, value) =>
  new Promise(
    resolve =>
      setTimeout(() => {
        console.log(value);
        resolve(value)
      }, howLong)
  );

Using Promise.resolve can be used as an initial value for example when you reduce values asynchronously:

[1,2,3].reduce(
  (all, item) =>
    all.then(
      () => later(2000, item)
    ),
  Promise.resolve()//initial value of all
)

Another common use case is if your function has to return a promise but can return a value immediately. Let's say you fetch some data and cache it when you got it. The next time you call the function you want it to return a promise because the caller is expecting a promise. You wrap the cached value in a promise:

const getData = (
  cache => () =>
    (cache)
      ? Promise.resolve(cache)
      : fetch("someURL").then(r=>r.json()).then(result=>{
        cache=result;
        return result;
      })
)(false)
HMR
  • 37,593
  • 24
  • 91
  • 160
  • 1
    Another common example for `Promise.resolve`: a function which may return cached data or retrieve data asynchronously, but which should obviously keep one uniform interface would return the cached data using `return Promise.resolve(data)` in order to always return a promise. – deceze Apr 23 '18 at 12:05
0

The only difference that I can see is that function b() returns the **whole promise and can take the accept and reject function from the **parameters of b function. Whereas, the function a() is a predefined Promise, which could have been passed into a() but all the same it's accept and reject are set in stone and returns those functions instead of the Promise Object.

Devin
  • 134
  • 9