0

Js is not my first language, by a long chalk. I code in 5 or 6 others, and am comfortable with callbacks, which means that I should be able to grok promises.

However, I inherited a node.js project with something like this (simplified), which makes me uncertain)

let promise = someFunction.then({return xxxx;});    

// lots of more code & promises

Promises.all().then(() => {return somethingElse});

I am comfortable with lots of promises and Promises.all() to wait for them all to resolve (although I am replacing all .then with await for cleaner code), BUT ...

that return inside a .then disturbs me. If I understand correctly, .then just wraps an async callback - which could happen at any time, even before the rest of the code and that Promises.all, leaving some of the code unexecuted.

Am I correct to be concerned, or is there something that I am missing?

Tl;dr - is it ok to return inside a .then?

Thinhbk
  • 2,194
  • 1
  • 23
  • 34
Mawg says reinstate Monica
  • 38,334
  • 103
  • 306
  • 551
  • 3
    _"is it ok to return inside a .then?"_ - absolutely. Why does it disturb you? --- _"just wraps an async callback"_ - no, an async function returns a promise. – evolutionxbox Mar 14 '22 at 13:54
  • 5
    _"is it ok to return inside a .then?"_ - How else would you transfer something from the `.then()` callback to the next callback in the chain? – Andreas Mar 14 '22 at 13:55
  • 2
    Returning a value from the `then` callback is [the core feature of promises](https://stackoverflow.com/a/22562045/1048572), so yes it's totally ok. – Bergi Mar 14 '22 at 13:56
  • Could you share your actual code, please? The snippet you posted doesn't really make sense. – Bergi Mar 14 '22 at 13:57

1 Answers1

3

Returning inside .then is extremely common and fine to do. What it does is it produces a Promise that now resolves to the value returned (rather than to the value returned by the previous Promise). Example:

// resolves to 1
const prom1 = Promise.resolve(1);

// chains off of prom1, resolves to 2
const prom2 = prom1.then(() => {
  return 2;
});

prom1.then(console.log);
setTimeout(() => {
  prom2.then(console.log);
});

This technique is very useful when you need to pass along a value from a prior .then to a subsequent .then.

const makeApiCall = num => Promise.resolve(num + 3);

Promise.resolve(1)
  .then((result1) => {
    return makeApiCall(result1);
  })
  .then((result2) => {
    console.log(result2);
  });

What the return will do in your situation specifically:

let promise = someFunction.then({return xxxx;});    

// lots of more code & promises

Promises.all().then(() => {return somethingElse});

If promise is passed to the Promise.all, then its resolve value will be xxxx, rather than whatever someFunction resolves to. (If the Promise.all's .then ignores its argument, then it does nothing except wait for the returned value to resolve, if it's a Promise.)

const someFunction = () => Promise.resolve(1);
let promise = someFunction().then(() => {
  return 'xxxx';
});

Promise.all([promise]).then((allResults) => {
    console.log('allResults', allResults);
    return 'somethingElse';
  })
  .then((result2) => {
    console.log('Next `.then`', result2);
  });

Note that your current code has a number of syntax issues you need to correct.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320