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

182
Visualizações
Difference between two ways of resolving a promise

I would like to understand the difference between the two code snippets, both resolving promises in different ways but receiving the same result (resolving after a second).

Which one is the right way?

function longTask(){
    return new Promise((resolve, reject) =>
    setTimeout(() => resolve(), 1000)
)}

longTask().then(() => console.log("done"));

function longTask(){
    return new Promise((resolve, reject) =>
    setTimeout(resolve, 1000)
)}

longTask().then(() => console.log("done"));

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

0

Both are technically the same.

The first solution runs an anonymous function that calls the resolve.

The second solution gets a reference to the resolve function that gets called in the end as well.

about 4 years ago · Juan Pablo Isaza Relatório

0

In this case, where you're not resolving with a value, there is no difference. In situations where you need to resolve the promise with a value, you would want to use the first example so you can control what is passed into the resolve function.

function longTask(){
    return new Promise((resolve, reject) =>
    setTimeout(() => resolve(someResolvedValue), 1000)
)}

longTask().then(() => console.log("done"));

Edit: Or you can use the second example and pass the resolved value as the third argument to setTimeout. thanks to VLAZ for pointing this out.

function longTask(){
    return new Promise((resolve, reject) =>
    setTimeout(resolve, 1000, someResolvedValue)
)}

longTask().then(() => console.log("done"));

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