Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

388
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda