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

430
Visualizações
what exactly does process.nextTick() do?

As per node.js documentation on "Understanding process.nextTick()"(https://nodejs.dev/learn/understanding-process-nexttick):

Every time the event loop takes a full trip, we call it a tick.

what does full trip mean?

In the below example code, since the process.nextTick() is added inside zoo() which is called inside foo(), shouldn't the function test("called from tick inside zoo") get called before boo()?

function test(msg) {
  console.log(msg);
}

function zoo() {
  console.log("Hello, I am zoo");

  process.nextTick(() => test("called from tick inside zoo"));
}

function foo() {
  console.log("Hello, I am foo");
  setTimeout(() => test("called from setTimeout foo"), 0);
  zoo();

}

function boo() {
  console.log("I am boo")
}

foo()
boo()

OUTPUT:

Hello, I am foo
Hello, I am zoo
I am boo
called from tick inside zoo
called from setTimeout foo

Also, in the same documentation page:

It's the way we can tell the JS engine to process a function asynchronously (after the current function), but as soon as possible, not queue it.

Does that mean that process.nextTick() basically ensures that the function passed gets priority over the queue(both message/job queue) but not in between the function calls within the call stack(as in foo() and boo() in the above example)?

If the above example does not apply the concept correctly. Please help me understand process.nextTick() with a simple example.

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

0

From the link in the question, this explains why:

When we pass a function to process.nextTick(), we instruct the engine to invoke this function at the end of the current operation, before the next event loop tick starts:

Why does "called from tick inside zoo" after "I am boo"?

boo() is called synchronously. This means it will be called in the current tick process.nextTick() will be called after the current tick:

we instruct the engine to invoke this function at the end of the current operation

Why is process.nextTick() callback called before setTimeout() callback?

Timeout callbacks will be called in a future tick, it could be the next tick or some ticks after the next tick. From the guide it says that process.nextTick() callbacks in between the current tick and the next tick:

before the next event loop tick starts

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