My express code:
app.use('/test', async (req, res, next) => {
try {
setTimeout(() => console.log('setTimeout')); // setTimeout (macrotask)
User.findOne().then(user => console.log('user')); // promise (microtask)
console.log('console.log');
} catch (err) {
next(err);
}
});
The console output order is:
console.log
setTimeout
user
Question: Why microtask is executed after macrotask?
For example, in the browser the next code resolves in correct order:
Code:
setTimeout(function timeout() {
console.log(3);
}, 0);
let p = new Promise(function (resolve, reject) {
for (let i = 0; i < 1e10; i++) {}
resolve();
});
p.then(function () {
console.log(2);
});
console.log(1);
Order:
1
2
3
The micro-task needs to be queued before the macro-task has been dequeued off the macro-task queue for it to run first. Since .findOne() takes some time, the micro-task isn't queued until the promise returned by .findOne() resolves, which happens after your setTimeout callback has been added to the macro-task queue, and then dequeue onto the call stack and executed.
Your "working" code is different to your situation in your Node program with .findOne(), since the executor function for the Promise you're creating runs synchronously (in fact you'll see that this code produces 1, 2, 3 in Node as well):
setTimeout(function timeout() { // <--- queue `timeout` callback as a macro-task
console.log(3);
}, 0);
let p = new Promise(function (resolve, reject) { // run this function synchronously
for (let i = 0; i < 1e10; i++) {} // <--- wait here for the loop to complete
resolve();
});
// Only reached once the loop above has complete, as thus, your promise has resolved
p.then(function () { // <--- `p` has resolved, so we queue the function as a micro-task
console.log(2);
});
console.log(1); // <--- add the function to call stack (first log to execute)
Above, while we're executing the script, both the setTimeout callback and the .then() callback are added to their respective task-queues so that once the script has finished executing, the micro-task can be dequeued and put onto the stack, and then the macro-task can be deqeued and put onto the stack.
Your code is different:
/*
setTimeout gets added to the callstack, that spins off an API which after 0 m/s adds your callback to the macro-task queue
*/
setTimeout(() => console.log('setTimeout'));
/*
.findOne() gets added to the call stack, that spins off an API which after N m/s adds the `.then()` callback to the micro-task queue (N > 0)
*/
User.findOne().then(user => console.log('user')); // promise (microtask)
/*
While the above API is working in the background, our script can continue on...
console.log() gets added to the call stack and "console.log" gets logged
*/
console.log('console.log');
Once the script above has finished, the timeout callback is on the macro-task queue, but the .then() callback isn't on the micro-task queue yet, as the query is still being executed in the background. The timeout callback then gets dequeued onto the call stack. After some time, your .then() callback is added to the micro-task queue and executed once the callstack is empty it gets moved from the queue to the stack.
Related
How can I ensure that the second function call is started after the first one finished execution?
const asyncFuntion = async (callNumber: string, timeout: number) => {
await setTimeout(() => {
console.log(`call: ${callNumber}`, timeout);
}, timeout);
};
asyncFuntion("1", 100);
asyncFuntion("2", 50);
Console output right now:
call: 2 50
call: 1 100
Desired console output:
call: 1 100
call: 2 50
await will have no effect on setTimeout() as it does not returns a promise.
So, the solution to this problem is to wrap setTimeout using Promise.
Also, if you want first function call to be executed before the second, create promises and resolve them before you make a call to second one and make use of await for the first function call.
Have a look at the following implementation:
const asyncFuntion = async (callNumber, timeout) => {
return new Promise( async (resolve, reject) => {
await setTimeout(() => {
console.log(`call: ${callNumber}`, timeout);
resolve();
}, timeout);
});
};
async function test() {
/*Write the function calls in the order you want them to get executed*/
await asyncFuntion("1", 100);
await asyncFuntion("2", 50);
}
test();
PS: You should also await the second call so that the promise returned by test() fulfills at the appropriate time, not in the middle of running the two timeouts. Credits #Bergi.
Two matters to notice.
Using await in front of setTimeout has no effect. It only works for functions which return a promise.
The execution order of the setTimeout solely depend on the interval - not on another iteration.
There is no way an interval of 100ms do something before the interval of 50ms. If you want the second call to be waiting until the completion of the first, create a promise and resolve it before you call the second one.
Another way of doing it to manipulate the setTimeout timer, its not as elegent but works.
let timer = 0
const asyncFuntion = (callNumber, timeout) => {
timer += timeout
setTimeout(() => {
console.log(`call: ${callNumber}`, timeout);
}, timer);
};
asyncFuntion("1", 100);
asyncFuntion("2", 50);
i was learning async library and just tried some codes myself and i am issueing a problem that can`t handle, can you please look at the code down below:)
async.parallel([
function (cb) {
setTimeout(() => {
let a = "asd";
console.log("AAA");
cb(a, null);
}, 2000);
},
function (cb) {
setTimeout( () => {
let b = "dasd";
console.log("BBBBB");
cb(b, null);
}, 5000);
}
], function (error, results) {
console.log("CCC");
console.log("Errors: " + error);
console.log("Results: " + results);
});
I supposed that BBB should NOT output to the screen, but to my surprise it DOES, can you help me understand why?
You are using async.parallel(). All asynchronous tasks will be executed without waiting for each other and the execution order is not guaranteed.
Here's a breakdown on how your script is executed:
Both setTimeout() are set.
2000 milliseconds later, console.log("AAA") and cb(a, null) are called.
cb(a, null) has an error. So the main callback is called, and async.parallel() ends.
But the story does not end here. The second setTimeout() is already set. Calling the main callback will not clear the timeout.
console.log("BBBBB") and cb(b, null) are called. This is why you see the output BBBBB.
Because the main callback is already called, calling cb(b, null) will not do anything.
function first(){
console.log('first')
}
function second(){
console.log('second')
}
let interval = async ()=>{
await setInterval(first,2000)
await setInterval(second,2000)
}
interval();
Imagine that I have this code above.
When I run it, first() and second() will be called at the same time; how do I call second() after first)() returns some data, for example, if first() is done, only then call second()?
Because first() in my code will be working with a big amount of data and if this 2 functions will be calling at the same time, it will be hard for the server.
How do I call second() each time when first() will return some data?
As mentioned above setInterval does not play well with promises if you do not stop it. In case you clear the interval you can use it like:
async function waitUntil(condition) {
return await new Promise(resolve => {
const interval = setInterval(() => {
if (condition) {
resolve('foo');
clearInterval(interval);
};
}, 1000);
});
}
Later you can use it like
const bar = waitUntil(someConditionHere)
You have a few problems:
Promises may only ever resolve once, setInterval() is meant to call the callback multiple times, Promises do not support this case well.
Neither setInterval(), nor the more appropriate setTimeout() return Promises, therefore, awaiting on them is pointless in this context.
You're looking for a function that returns a Promise which resolves after some times (using setTimeout(), probably, not setInterval()).
Luckily, creating such a function is rather trivial:
async function delay(ms) {
// return await for better async stack trace support in case of errors.
return await new Promise(resolve => setTimeout(resolve, ms));
}
With this new delay function, you can implement your desired flow:
function first(){
console.log('first')
}
function second(){
console.log('second')
}
let run = async ()=>{
await delay(2000);
first();
await delay(2000)
second();
}
run();
setInterval doesn't play well with promises because it triggers a callback multiple times, while promise resolves once.
It seems that it's setTimeout that fits the case. It should be promisified in order to be used with async..await:
async () => {
await new Promise(resolve => setTimeout(() => resolve(first()), 2000));
await new Promise(resolve => setTimeout(() => resolve(second()), 2000));
}
await expression causes async to pause until a Promise is settled
so you can directly get the promise's result without await
for me, I want to initiate Http request every 1s
let intervalid
async function testFunction() {
intervalid = setInterval(() => {
// I use axios like: axios.get('/user?ID=12345').then
new Promise(function(resolve, reject){
resolve('something')
}).then(res => {
if (condition) {
// do something
} else {
clearInterval(intervalid)
}
})
}, 1000)
}
// you can use this function like
testFunction()
// or stop the setInterval in any place by
clearInterval(intervalid)
You could use an IFFE. This way you could escape the issue of myInterval not accepting Promise as a return type.
There are cases where you need setInterval, because you want to call some function unknown amount of times with some interval in between.
When I faced this problem this turned out to be the most straight-forward solution for me. I hope it help someone :)
For me the use case was that I wanted to send logs to CloudWatch but try not to face the Throttle exception for sending more than 5 logs per second. So I needed to keep my logs and send them as a batch in an interval of 1 second. The solution I'm posting here is what I ended up using.
async function myAsyncFunc(): Promise<string> {
return new Promise<string>((resolve) => {
resolve("hello world");
});
}
function myInterval(): void {
setInterval(() => {
void (async () => {
await myAsyncFunc();
})();
}, 5_000);
}
// then call like so
myInterval();
Looked through all the answers but still didn't find the correct one that would work exactly how the OP is asked. This is what I used for the same purpose:
async function waitInterval(callback, ms) {
return new Promise(resolve => {
let iteration = 0;
const interval = setInterval(async () => {
if (await callback(iteration, interval)) {
resolve();
clearInterval(interval);
}
iteration++;
}, ms);
});
}
function first(i) {
console.log(`first: ${i}`);
// If the condition below is true the timer finishes
return i === 5;
}
function second(i) {
console.log(`second: ${i}`);
// If the condition below is true the timer finishes
return i === 5;
}
(async () => {
console.log('start');
await waitInterval(first, 1000);
await waitInterval(second, 1000);
console.log('finish');
})()
In my example, I also put interval iteration count and the timer itself, just in case the caller would need to do something with it. However, it's not necessary
In my case, I needed to iterate through a list of images, pausing in between each, and then a longer pause at the end before re-looping through.
I accomplished this by combining several techniques from above, calling my function recursively and awaiting a timeout.
If at any point another trigger changes my animationPaused:boolean, my recursive function will exit.
const loopThroughImages = async() => {
for (let i=0; i<numberOfImages; i++){
if (animationPaused) {
return;
}
this.updateImage(i);
await timeout(700);
}
await timeout(1000);
loopThroughImages();
}
loopThroughImages();
Async/await do not make the promises synchronous.
To my knowledge, it's just a different syntax for return Promise and .then().
Here i rewrote the async function and left both versions, so you can see what it really does and compare.
It's in fact a cascade of Promises.
// by the way no need for async there. the callback does not return a promise, so no need for await.
function waitInterval(callback, ms) {
return new Promise(resolve => {
let iteration = 0;
const interval = setInterval(async () => {
if (callback(iteration, interval)) {
resolve();
clearInterval(interval);
}
iteration++;
}, ms);
});
}
function first(i) {
console.log(`first: ${i}`);
// If the condition below is true the timer finishes
return i === 5;
}
function second(i) {
console.log(`second: ${i}`);
// If the condition below is true the timer finishes
return i === 5;
}
// async function with async/await, this code ...
(async () => {
console.log('start');
await waitInterval(first, 1000);
await waitInterval(second, 1000);
console.log('finish');
})() //... returns a pending Promise and ...
console.log('i do not wait');
// ... is kinda identical to this code.
// still asynchronous but return Promise statements with then cascade.
(() => {
console.log('start again');
return waitInterval(first, 1000).then(() => {
return waitInterval(second, 1000).then(() => {
console.log('finish again');
});
});
})(); // returns a pending Promise...
console.log('i do not wait either');
You can see the two async functions both execute at the same time.
So using promises around intervals here is not very useful, as it's still just intervals, and promises changes nothing, and make things confusing...
As the code is calling callbacks repeatedly into an interval, this is, i think, a cleaner way:
function first(i) {
console.log(`first: ${i}`);
// If the condition below is true the timer finishes
return i === 5;
}
function second(i) {
console.log(`second: ${i}`);
// If the condition below is true the timer finishes
return i === 5;
}
function executeThroughTime(...callbacks){
console.log('start');
let callbackIndex = 0; // to track current callback.
let timerIndex = 0; // index given to callbacks
let interval = setInterval(() =>{
if (callbacks[callbackIndex](timerIndex++)){ // callback return true when it finishes.
timerIndex = 0; // resets for next callback
if (++callbackIndex>=callbacks.length){ // if no next callback finish.
clearInterval(interval);
console.log('finish');
}
}
},1000)
}
executeThroughTime(first,second);
console.log('and i still do not wait ;)');
Also, this solution execute a callback every secondes.
if the callbacks are async requests that takes more than one sec to resolve, and i can't afford for them to overlap, then, instead of doing iterative call with repetitive interval, i would get the request resolution to call the next request (through a timer if i don't want to harass the server).
Here the "recursive" task is called lTask, does pretty much the same as before, except that, as i do not have an interval anymore, i need a new timer each iteration.
// slow internet request simulation. with a Promise, could be a callback.
function simulateAsync1(i) {
console.log(`first pending: ${i}`);
return new Promise((resolve) =>{
setTimeout(() => resolve('got that first big data'), Math.floor(Math.random()*1000)+ 1000);//simulate request that last between 1 and 2 sec.
}).then((result) =>{
console.log(`first solved: ${i} ->`, result);
return i==2;
});
}
// slow internet request simulation. with a Promise, could be a callback.
function simulateAsync2(i) {
console.log(`second pending: ${i}`);
return new Promise((resolve) =>{
setTimeout(() => resolve('got that second big data'), Math.floor(Math.random()*1000) + 1000);//simulate request that last between 1 and 2 sec.
}).then((result) =>{ // promise is resolved
console.log(`second solved: ${i} ->`,result);
return i==4; // return a promise
});
}
function executeThroughTime(...asyncCallbacks){
console.log('start');
let callbackIndex = 0;
let timerIndex = 0;
let lPreviousTime = Date.now();
let lTask = () => { // timeout callback.
asyncCallbacks[callbackIndex](timerIndex++).then((result) => { // the setTimeout for the next task is set when the promise is solved.
console.log('result',result)
if (result) { // current callback is done.
timerIndex = 0;
if (++callbackIndex>=asyncCallbacks.length){//are all callbacks done ?
console.log('finish');
return;// its over
}
}
console.log('time elapsed since previous call',Date.now() - lPreviousTime);
lPreviousTime = Date.now();
//console.log('"wait" 1 sec (but not realy)');
setTimeout(lTask,1000);//redo task after 1 sec.
//console.log('i do not wait');
});
}
lTask();// no need to set a timer for first call.
}
executeThroughTime(simulateAsync1,simulateAsync2);
console.log('i do not wait');
Next step would be to empty a fifo with the interval, and fill it with web request promises...
CODE -
.then (() => {
console.log("Wait");
setTimeout(function(){console.log("Wait to process")},1500);
this.timeout(2000);
})
.then(() => {
console.log("Get ABC");
return common.getApiData(url)})
Now when i run this code it logs data like -
Wait
Get ABC
Wait to process
(The it waits for the time specified above)
I want to put timeout before calling getApiData method..
Supposing you are using real promises, this is the function you should pass to then:
.then(function (value) {
var p = new Promise ();
setTimeout (function () {
p.resolve(value)
}, 2000)
return p
})
The next then will be called once the promise is resolved.
I'm very confused about the differences between nextTick and setImmediate. I've read all the documentation about them on the internet but I still don't understand how they work.
Examples:
function log(n) { console.log(n); }
setImmediate
setImmediate(function() {
setImmediate(function() {
log(1);
setImmediate(function() { log(2); });
setImmediate(function() { log(3); });
});
setImmediate(function() {
log(4);
setImmediate(function() { log(5); });
setImmediate(function() { log(6); });
});
});
//1 2 3 4 5 6
nextTick
process.nextTick(function() {
process.nextTick(function() {
log(1);
process.nextTick(function() { log(2); });
process.nextTick(function() { log(3); });
});
process.nextTick(function() {
log(4);
process.nextTick(function() { log(5); });
process.nextTick(function() { log(6); });
});
});
//1 4 2 3 5 6
Why these results? Please explain with a visual or very easy to follow explanation. Even the node core devs don't agree at how nextTick and setImmediate should be understood by people.
Sources:
setImmediate vs. nextTick
Why is setImmediate much more slower than nextTick?
setImmediate is not always very immediate
Consider the following two examples:
setImmediate
setImmediate(function A() {
setImmediate(function B() {
log(1);
setImmediate(function D() { log(2); });
setImmediate(function E() { log(3); });
});
setImmediate(function C() {
log(4);
setImmediate(function F() { log(5); });
setImmediate(function G() { log(6); });
});
});
setTimeout(function timeout() {
console.log('TIMEOUT FIRED');
}, 0)
// 'TIMEOUT FIRED' 1 4 2 3 5 6
// OR
// 1 'TIMEOUT FIRED' 4 2 3 5 6
nextTick
process.nextTick(function A() {
process.nextTick(function B() {
log(1);
process.nextTick(function D() { log(2); });
process.nextTick(function E() { log(3); });
});
process.nextTick(function C() {
log(4);
process.nextTick(function F() { log(5); });
process.nextTick(function G() { log(6); });
});
});
setTimeout(function timeout() {
console.log('TIMEOUT FIRED');
}, 0)
// 1 4 2 3 5 6 'TIMEOUT FIRED'
setImmediate callbacks are fired off the event loop, once per iteration in the order that they were queued. So on the first iteration of the event loop, callback A is fired. Then on the second iteration of the event loop, callback B is fired, then on the third iteration of the event loop callback C is fired, etc. This prevents the event loop from being blocked and allows other I/O or timer callbacks to be called in the mean time (as is the case of the 0ms timeout, which is fired on the 1st or 2nd loop iteration).
nextTick callbacks, however, are always fired immediately after the current code is done executing and BEFORE going back to the event loop. In the nextTick example, we end up executing all the nextTick callbacks before ever returning to the event loop. Since setTimeout's callback will be called from the event loop, the text 'TIMEOUT FIRED' will not be output until we're done with every nextTick callback.
According the Node.js doc names of these two function are exactly swapped
setImmediate() (BEST RECOMMENDED)
It's fire first at event queue
process.nextTick() (USE FOR SPECIAL CASES see example later on)
It's fire immediately, It's kinda write an statement more at the end at the current file
If we have this code
setTimeout(function(){
console.log('Hello world 5'); // It's waiting like a normal person at a queue
}, 0);
setImmediate(function(){
console.log('Hello world 4');
// It's like get to last and be take care of first
// but always after of .nextTick and before of setInterval(, 0)
});
process.nextTick(function(){
console.log('Hello world 3'); // It's like be at the bottom at this file
});
console.log('Hello world 1');
console.log('Hello world 2');
A visual explanation as per your request:
Cases for use process.nextTick() when you have to emit and event before to handled it:
const EventEmitter = require('events');
const util = require('util');
function MyEmitter() {
EventEmitter.call(this);
// use nextTick to emit the event once a handler is assigned
process.nextTick(function () {
this.emit('event');
}.bind(this));
}
util.inherits(MyEmitter, EventEmitter);
const myEmitter = new MyEmitter();
myEmitter.on('event', function() {
console.log('an event occurred!');
});
Look at this vide where Philip Roberts give us a great explanation about runtime event loop and look at this online eventloop debugger Live test how event loop works
Source:
https://github.com/nodejs/node/blob/master/doc/topics/the-event-loop-timers-and-nexttick.md#processnexttick-vs-setimmediate
I can't reproduce your results for setImmediate. It should be the same as nextTick (and it is in my tests) since in this situation they do pretty much the same thing. The only reasonable explanation for that is that setImmediate is somehow synchronous, but it is not.
And according to NodeJS documentation the only real difference is that multiple nextTick may fire in one loop iteration (depending on maxTickDepth), while setImmediate fires once per iteration.
Below gives you better clarity.
setImmediate
It's execute a script once the current poll phase completes.
It's a timer module function and timer functions are global, you can call them without require.
It can cleared by clearImmediate().
Set "immediate" execution of the callback after I/O events' callbacks before setTimeout() and setInterval().
nextTick
It's a process global object function of NodeJS.
All callbacks passed to process.nextTick() will be resolved before the event loop continues.
Allow users to handle errors.
Helps to try the request again before the event loop continues.
Simple code Snippet.
console.log("I'm First");
setImmediate(function () {
console.log('Im setImmediate');
});
console.log("I'm Second");
process.nextTick(function () {
console.log('Im nextTick');
});
console.log("I'm Last");
/*
Output
$ node server.js
I'm First
I'm Second
I'm Last
Im nextTick
Im setImmediate
*/
I think all the answers above are obsolete, because I got different answers constantly with the current version of nodejs and it is easy to reason about
var log=console.log
log(process.version)
var makeAsyncCall
if(false)
makeAsyncCall=setImmediate
else
makeAsyncCall=process.nextTick;
makeAsyncCall(function A () {
makeAsyncCall(function B() {
log(1);
makeAsyncCall(function C() { log(2); });
makeAsyncCall(function D() { log(3); });
});
makeAsyncCall(function E() {
log(4);
makeAsyncCall(function F() { log(5); });
makeAsyncCall(function G() { log(6); });
});
});
//1
//4
//2
//3
//5
//6
//in both case
After reading https://github.com/nodejs/node/blob/master/doc/topics/the-event-loop-timers-and-nexttick.md#processnexttick-vs-setimmediate
let use start from setImmediate we should keep track of the check queue because it is where the setImmediate callback reside.
First iteration
A is push to check queue
check queue :[A]
Second iteration
A is pull out from queue to execute
During its execution ,it put B and E to queueand then, A complete and start next iteration
check queue :[B,E]
Third iteration
pull out B and push C D
check queue :[E,C,D]
Forth iteration
pull out E and push F G
check queue : [C,D,F,G]
Finally
execute the callbacks in the queue sequentially
For nextTick case, the queue works exactly the same way,that is why it produce same result
The different is that :
the nextTickQueue will be processed after the current operation
completes, regardless of the current phase of the event loop
To be clear,the event loop maintain multiple queues and check queue is just one of them,the node will decide which queue to use based on some rules
with process.nextTick however,it is sort of bypassing all the rule and execute the callback in nextTick immediately