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

191
Visualizações
Is there a better alternative to setTimeOut(fn, 0)?

Is there another way of achieving the same behavior of scheduling a callback function on the JavaScript's message queue to be run after the current stack is empty?

In other words, is there a way, be it with Promises or third-party packages to push a callback to the task queue so that it runs after the current stack is empty?

In other words, what are some equivalents/alternatives to passing 0 to setTimeout to take advantage of the asynchronous callback?

Answers for all environments and all ECMAScript versions are welcome.

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

0

In nodejs environment, you can do the async tasks in the following way.

Ref. https://nodejs.org/en/docs/guides/event-loop-timers-and-nexttick/

setImmediate(() => console.log("setImmediate"));
setTimeout(() => console.log("setTimeout"));
Promise.resolve().then(() => console.log("Promise"));
process.nextTick(() => console.log("nextTick"));
console.log("sync");

Output:

sync
nextTick
Promise
setTimeout
setImmediate

In modern browswer, you can do the async tasks in the following way.

setTimeout(() => console.log("setTimeout"));
Promise.resolve().then(() => console.log("Promise"));
console.log("sync");

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