Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

378
Views
Return in .then(), when there are multiple .then()?

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?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!