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

247
Visualizações
Is passing a Promise as a function argument considered a bad practise?

I have often asked myself about this, since it can lead to the obvious problem:

function someFunction (a, b) {
   return a + b
}

If a is a promise you would need to resolve it first:

async function someFunction (a, b) {
   a = await a
   return a + b
}

Leading to confusing on whether a is resolved or not...

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Yes, one should generally avoid passing promises to functions. Prefer waiting for the promise and passing only the result, i.e. do

somethingAsync().then(x => someFunction(x, y))
// or
someFunction(await somethingAsync(), y)

instead of

someFunction(somethingAsync(), y)

As you say, it's very confusing otherwise, especially when not using TypeScript to tell you when you have messed up. And the implementation of someFunction becomes much simpler when it is synchronous and doesn't have to deal with any asynchronous logic, which also makes it more universally useful.

Of course, there are exceptions to every rule, and they would be functions that are explicitly dedicated to handle asynchronous promise logic, like Promise.resolve, Promise.all, Promise.race, .then() etc. Sometimes you write similar helper functions yourself, e.g. for error-handling, and in those cases it might be acceptable to pass a promise. It's still unusual though.

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