Questions tagged [setimmediate]

Node JS's SetImmediate schedules the "immediate" execution of the callback after I/O events' callbacks and before timers created using setTimeout() and setInterval() are triggered. Returns an Immediate for use with clearImmediate().

setImmediate(callback[, ...args])

callback The function to call at the end of this turn of the Node.js Event Loop ...args Optional arguments to pass when the callback is called.

When multiple calls to setImmediate() are made, the callback functions are queued for execution in the order in which they are created. The entire callback queue is processed every event loop iteration. If an immediate timer is queued from inside an executing callback, that timer will not be triggered until the next event loop iteration.

If callback is not a function, a TypeError will be thrown.


Example of setImmediate

This will output hello \n world!

    var saywhat = 'hello';
    setImmediate(function(value) {
    console.log(saywhat);
    console.log(value);
    }, "world!");
33 questions
385
votes
8 answers

setImmediate vs. nextTick

Node.js version 0.10 was released today and introduced setImmediate. The API changes documentation suggests using it when doing recursive nextTick calls. From what MDN says it seems very similar to process.nextTick. When should I use nextTick and…
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
4
votes
0 answers

Node.js Event-Loop. Why callbacks from the check queue execute before those from the poll queue while Node.js DOCs state vice versa?

According to the Node.js DOCs, when the event-loop enters its poll phase and the poll queue is not empty, the callbacks in the poll queue should get executed before the event-loop proceeds further to its check phase. In reality, however, the…
Igor C.
  • 41
  • 2
4
votes
1 answer

Jest: await vs setImmediate vs useFakeTimers vs new Promise(setImmediate)

What follows is a Jest test in TypeScript. I'm wondering why setImmediate() is required. The first example is a test that works. Next are various things I've tried that don't work. I'm not understanding the what is going on. The signature for…
Jeff Lowery
  • 2,492
  • 2
  • 32
  • 40
3
votes
0 answers

How to polyfill setImmediate?

I use graphql's dataloader in a project. Unfortunately, it breaks Webpack 5 because: setImmediate is not defined I see where the issue is coming from in their source code. I forked the repo, made a patch and added it to my package.json. But the…
DoneDeal0
  • 5,273
  • 13
  • 55
  • 114
3
votes
1 answer

How to promisify and await for setImmediate in Node?

I've been reading about how to not block Node's event loop. One way to avoid blocking is to use partitioning. I'm trying to use a partitioned loop in my code, but I cannot seem to await for my loop. Here's a simplified version of my code: const…
iepure
  • 249
  • 2
  • 14
2
votes
3 answers

In Node.js, setImmediate() callbacks get executed on current event loop tick, or on the next one?

In the official docs, "setImmediate() vs setTimeout()" section, it says: "setImmediate() is designed to execute a script once the current poll phase completes", and as i understand, it means also current tick/event loop cycle. However later in the…
Roy
  • 31
  • 1
  • 4
2
votes
1 answer

Why does setImmediate() run faster than sequential code?

I'm learning how setImmediate() works and came accross an oddity that I fail to find a technical explanation for. The code is really simple: setImmediate(function(){ console.log("third", process.hrtime() ); }); console.log("first",…
John Doe
  • 67
  • 4
2
votes
1 answer

Why Node.js setImmediate executes after I/O callbacks?

As new member, I'm unable to comment on topics, that's why I had to create a new topic. But in this way, I can clarify the problem, so hopefully you guys can help me. I have read quite a lot about Node.js Event Loop. And I have shaped my…
Mahdi
  • 1,089
  • 1
  • 14
  • 27
2
votes
1 answer

'this' lost in setImmediate call?

I have a situation where a method must call itself recursively, and asynchronously, via setImmediate. The next code doesn't have any to do with the original, but the same "problem" arises: the "this" reference seems to be lost on the second…
Emilio Grisolía
  • 1,183
  • 1
  • 9
  • 16
1
vote
2 answers

NodeJS setImmediate loop: Is only one setImmediate callback executed per iteration of the event loop?

function loop() { // Anything you want to run in a loop can be here setImmediate(loop); } loop(); In this case, a setImmediate callback is calling another setImmediate whose callback is eventually to the queue (of the "Check" phase). Thus…
regna
  • 11
  • 2
1
vote
0 answers

Why are the results not different when there's code in global?

Code Snippet-1 setTimeout(() => console.log("setTimeout1")); setTimeout(() => console.log("setTimeout2")); setImmediate(() => console.log("setImmediate1")); setImmediate(() => console.log("setImmediate2")); Code Snippet-2 setTimeout(() =>…
RedPotato
  • 37
  • 5
1
vote
1 answer

NodeJS setImmediate and private class method

So, first time experimenting with setImmediate. It seems it can't run private class methods. Is that so? If yes, can someone explain to me why? Doesn't work Private class method as argument to setImmediate. Throws Callback must be a function.…
Nitin Bansal
  • 2,986
  • 3
  • 23
  • 30
1
vote
1 answer

Determine promise state using Promise.race and setImmediate

TL;DR: Will already resolved promises always beat setImmediate in a race? Background: Sometimes you want to know if a promise is resolved without awaiting its completion. There are some old legacy tricks like using util.inspect to get the internal…
JHH
  • 8,567
  • 8
  • 47
  • 91
1
vote
1 answer

Using setImmediate to improve JSON.stringify performance of large amounts of data

I have some code that JSON.stringify's an array of objects like so: const postsCommentsReadyForDB = postsComments.map(postComments => ({ id: getPostId(postComments), comments: JSON.stringify(getComments(postComments)), })) However on very large…
Redark
  • 173
  • 1
  • 11
1
vote
1 answer

Nodejs setImmediate() function Realtime usecase and example

Can some some one please explain the realtime usecase of setImmediate() function in nodejs.I've gone through many blogs but every where they gave console.log example with setImmediate.
1
2 3