Here a == 3 and the condition was true but when the setinterval runs, the value of a will be 4 then, do while will check the condition and it will be a false condition, though this will not run.
I don't see any error in this.
setinterval was blocking the next condition which will be verified but it's an infinite loop which does not end so the checking of condition will not happen but do while was executed first without checking the condition and the inside if condition was true will execute the setinterval method but the confusion created in the definition of asynchronous function definition is that (it will be multi-threaded so the execution of both will run simultaneously it will hold the execution setinterval and execute the upcoming statements) by following the definition it will check condition first but this will prove wrong the definition of do while loop because in do while the condition was checked in 2nd iteration 1st iteration will be executed without checking the condition of the loop.
var a=3;
function ali() {
console.log("testing and understanding setInterval method");
a++;
}
do {
if (a == 3)
setInterval(ali,1000);
} while (a == 3);
setInterval should run but being on asynchronous function it is running with no output. When running it it says heap stack is full in Node.js.
JavaScript is not multi-threaded. You essentially have:
condition = true;
do {
setInterval(some_function_that_changes_condition, 1000)
} while(condition);
You're right that this is an infinite loop.
This is like saying "I will continuously order pizzas until one arrives". So you start ordering pizza-after-pizza on the phone but you never answer the door when one of the pizzas actually arrives because you're busy making phone-calls to order more pizzas.
setInterval basically schedules a function to be run later, but nothing else can happen in JavaScript until you relinquish control of the JavaScript context by returning from the function that this code was written in. There is no way for JavaScript to interrupt execution of your loop to execute the callback. Maybe you're thinking that it might happen during one of the setInterval calls? But it won't.
In Node.js the timer may very well be scheduled and the thread that wants to invoke the callback function may be runnable, but the JavaScript engine itself is busy running your infinite loop and cannot be interrupted.
The timer functions never guarantee that they would run precisely after the interval. It literally says that "I will execute it soon as I get the breathing space after the specified time".
What is breathing space? Every time a function runs, the JS engine waits for the function to return. This is the breathing space. It is at that moment the JS engine checks its "Event Loop" what it has to execute next. This is the only time your timer function may be scheduled to run.
The timer never runs before the long loop is over in the following example.
function test() {
console.log('Starting')
setTimeout(()=> console.log('timer executed'), 1000)
for (let i=1; i<=9999999999; i++);
console.log('the loop is over');
}
test()
Related
setTimeout(function(){
console.log('hari');
process.exit()
}, 300);
for(i=0;i<3000000;i++) {
console.log(i);
}
Please explain why setTimeout is not finished in 300 ms here. setTimeout is executing only after the for loop completes; why?
As you might already know, Node.js is using the V8 (JavaScript) engine which is single threaded. The core of I/O execution is around an event-loop; when you block the event loop it also blocks execution of other events.
Basically, the for loop is executed before, and it starts blocking the event loop. The setTimeout does not guarantee that your code gets executed exactly after 300 ms, but more like >= 300 ms. To get a better understanding of event loop read this.
Basically, your code runs in a single thread. setTimeout has lower priority than your standard code, so it will execute first and then (because the thread will be finished) it will execute your function from setTimeout.
setTimeout(milliseconds) runs a function no faster than in the specified number of milliseconds.
Assume makeBurger() will take 10 seconds
In synchronous program,
function serveBurger() {
makeBurger();
makeBurger();
console.log("READY") // Assume takes 5 seconds to log.
}
This will take a total of 25 seconds to execute.
So for NodeJs lets say we make an async version of makeBurgerAsync() which also takes 10 seconds.
function serveBurger() {
makeBurgerAsync(function(count) {
});
makeBurgerAsync(function(count) {
});
console.log("READY") // Assume takes 5 seconds to log.
}
Since it is a single thread. I have troubling imagine what is really going on behind the scene.
So for sure when the function run, both async functions will enter event loops and console.log("READY") will get executed straight away.
But while console.log("READY") is executing, no work is really done for both async function right? Since single thread is hogging console.log for 5 seconds.
After console.log is done. CPU will have time to switch between both async so that it can run a bit of each function each time.
So according to this, the function doesn't necessarily result in faster execution, async is probably slower due to switching between event loop? I imagine that, at the end of the day, everything will be spread on a single thread which will be the same thing as synchronous version?
I am probably missing some very big concept so please let me know. Thanks.
EDIT
It makes sense if the asynchronous operations are like query DB etc. Basically nodejs will just say "Hey DB handle this for me while I'll do something else". However, the case I am not understanding is the self-defined callback function within nodejs itself.
EDIT2
function makeBurger() {
var count = 0;
count++; // 1 time
...
count++; // 999999 times
return count;
}
function makeBurgerAsync(callback) {
var count = 0;
count++; // 1 time
...
count++; // 999999 times
callback(count);
}
In node.js, all asynchronous operations accomplish their tasks outside of the node.js Javascript single thread. They either use a native code thread (such as disk I/O in node.js) or they don't use a thread at all (such as event driven networking or timers).
You can't take a synchronous operation written entirely in node.js Javascript and magically make it asynchronous. An asynchronous operation is asynchronous because it calls some function that is implemented in native code and written in a way to actually be asynchronous. So, to make something asynchronous, it has to be specifically written to use lower level operations that are themselves asynchronous with an asynchronous native code implementation.
These out-of-band operations, then communicate with the main node.js Javascript thread via the event queue. When one of these asynchronous operations completes, it adds an event to the Javascript event queue and then when the single node.js thread finishes what it is currently doing, it grabs the next event from the event queue and calls the callback associated with that event.
Thus, you can have multiple asynchronous operations running in parallel. And running 3 operations in parallel will usually have a shorter end-to-end running time than running those same 3 operations in sequence.
Let's examine a real-world async situation rather than your pseudo-code:
function doSomething() {
fs.readFile(fname, function(err, data) {
console.log("file read");
});
setTimeout(function() {
console.log("timer fired");
}, 100);
http.get(someUrl, function(err, response, body) {
console.log("http get finished");
});
console.log("READY");
}
doSomething();
console.log("AFTER");
Here's what happens step-by-step:
fs.readFile() is initiated. Since node.js implements file I/O using a thread pool, this operation is passed off to a thread in node.js and it will run there in a separate thread.
Without waiting for fs.readFile() to finish, setTimeout() is called. This uses a timer sub-system in libuv (the cross platform library that node.js is built on). This is also non-blocking so the timer is registered and then execution continues.
http.get() is called. This will send the desired http request and then immediately return to further execution.
console.log("READY") will run.
The three asynchronous operations will complete in an indeterminate order (whichever one completes it's operation first will be done first). For purposes of this discussion, let's say the setTimeout() finishes first. When it finishes, some internals in node.js will insert an event in the event queue with the timer event and the registered callback. When the node.js main JS thread is done executing any other JS, it will grab the next event from the event queue and call the callback associated with it.
For purposes of this description, let's say that while that timer callback is executing, the fs.readFile() operation finishes. Using it's own thread, it will insert an event in the node.js event queue.
Now the setTimeout() callback finishes. At that point, the JS interpreter checks to see if there are any other events in the event queue. The fs.readfile() event is in the queue so it grabs that and calls the callback associated with that. That callback executes and finishes.
Some time later, the http.get() operation finishes. Internal to node.js, an event is added to the event queue. Since there is nothing else in the event queue and the JS interpreter is not currently executing, that event can immediately be serviced and the callback for the http.get() can get called.
Per the above sequence of events, you would see this in the console:
READY
AFTER
timer fired
file read
http get finished
Keep in mind that the order of the last three lines here is indeterminate (it's just based on unpredictable execution speed) so that precise order here is just an example. If you needed those to be executed in a specific order or needed to know when all three were done, then you would have to add additional code in order to track that.
Since it appears you are trying to make code run faster by making something asynchronous that isn't currently asynchronous, let me repeat. You can't take a synchronous operation written entirely in Javascript and "make it asynchronous". You'd have to rewrite it from scratch to use fundamentally different asynchronous lower level operations or you'd have to pass it off to some other process to execute and then get notified when it was done (using worker processes or external processes or native code plugins or something like that).
When we are using Promise in nodejs, given a Promise p, we can't know when the Promise p is actually resolved by logging the currentTime in the "then" callback.
To prove that, I wrote the test code below (using CoffeeScript):
# the procedure we want to profile
getData = (arg) ->
new Promise (resolve) ->
setTimeout ->
resolve(arg + 1)
, 100
# the main procedure
main = () ->
beginTime = new Date()
console.log beginTime.toISOString()
getData(1).then (r) ->
resolveTime = new Date()
console.log resolveTime.toISOString()
console.log resolveTime - beginTime
cnt = 10**9
--cnt while cnt > 0
return cnt
main()
When you run the above code, you will notice that the resolveTime (the time your code run into the callback function) is much later than 100ms from the beginTime.
So If we want to know when the Promise is actually resolved, HOW?
I want to know the exact time because I'm doing some profiling via logging. And I'm not able to modify the Promise p 's implementation when I'm doing some profiling outside of the black box.
So, Is there some function like promise.onUnderlyingConditionFulfilled(callback) , or any other way to make this possible?
This is because you have a busy loop that apparently takes longer than your timer:
cnt = 10**9
--cnt while cnt > 0
Javascript in node.js is single threaded and event driven. It can only do one thing at a time and it will finish the current thing it's doing before it can service the event posted by setTimeout(). So, if your busy loop (or any other long running piece of Javascript code) takes longer than you've set your timer for, the timer will not be able to run until this other Javascript code is done. "single threaded" means Javascript in node.js only does one thing at a time and it waits until one thing returns control back to the system before it can service the next event waiting to run.
So, here's the sequence of events in your code:
It calls the setTimeout() to schedule the timer callback for 100ms from now.
Then you go into your busy loop.
While it's in the busy loop, the setTimeout() timer fires inside of the JS implementation and it inserts an event into the Javascript event queue. That event can't run at the moment because the JS interpreter is still running the busy loop.
Then eventually it finishes the busy loop and returns control back to the system.
When that is done, the JS interpreter then checks the event queue to see if any other events need servicing. It finds the timer event and so it processes that and the setTimeout() callback is called.
That callback resolves the promise which triggers the .then() handler to get called.
Note: Because of Javascript's single threaded-ness and event-driven nature, timers in Javascript are not guaranteed to be called exactly when you schedule them. They will execute as close to that as possible, but if other code is running at the time they fire or if their are lots of items in the event queue ahead of you, that code has to finish before the timer callback actually gets to execute.
So If we want to know when the Promise is actually resolved, HOW?
The promise is resolved when your busy loop is done. It's not resolved at exactly the 100ms point (because your busy loop apparently takes longer than 100ms to run). If you wanted to know exactly when the promise was resolved, you would just log inside the setTimeout() callback right where you call resolve(). That would tell you exactly when the promise was resolved though it will be pretty much the same as where you're logging now. The cause of your delay is the busy loop.
Per your comments, it appears that you want to somehow measure exactly when resolve() is actually called in the Promise, but you say that you cannot modify the code in getData(). You can't really do that directly. As you already know, you can measure when the .then() handler gets called which will probably be no more than a couple ms after resolve() gets called.
You could replace the promise infrastructure with your own implementation and then you could instrument the resolve() callback directly, but replacing or hooking the promise implementation probably influences the timing of things even more than just measuring from the .then() handler.
So, it appears to me that you've just over-constrained the problem. You want to measure when something inside of some code happens, but you don't allow any instrumentation inside that code. That just leaves you with two choices:
Replace the promise implementation so you can instrument resolve() directly.
Measure when .then() is triggered.
The first option probably has a heisenberg uncertainty issue in that you've probably influenced the timing by more than you should if you replace or hook the promise implementation.
The second option measures an event that happens just slightly after the actual .resolve(). Pick which one sounds closest to what you actually want.
code1:
function foo() {
console.log('foo');
}
process.nextTick(foo);
console.log('bar');
code2:
function foo() {
console.log('foo');
}
foo();
console.log('bar');
The first piece of code output:
bar
foo
and the second is opposite:
foo
bar
why?
My question is :
How to understand "what process.nextTick() actually does is defer the execution of an action till the next pass around the event loop"
Actually, I don't know the concept of 'event', console.log('bar') is an event? foo() is an event? process.nextTick(foo) is an event?
Thank you guys.
process.nextTick(foo) schedules foo() to be called after the current thread of JS execution finishes. It does this by essentially inserting a call to foo() at the front of the event queue. When the current thread of JS execution finishes, the node.js engine will pull the next event off the event queue and run it. This will cause you to get your output:
bar
foo
Because console.log('bar') is part of the current thread of execution so that will finish before the next event in the event loop is serviced.
So, in words, here's what the code1 example is doing:
Define foo function (this is actually done by the parser before anything executes)
Run process.nextTick(foo) which schedules foo() to be run after the rest of this thread of Javascript finishes by inserting an event in the event queue. All callbacks or functions to be run in the future are scheduled this way in node.js (via the event queue). node.js is an event driven environment.
Run console.log('bar').
Current thread of JS finishes execution.
System pulls next event from the event queue and runs it.
This next event is the call to foo() from the process.nextTick() call.
foo() runs.
In your code2 example, this is just sequential execution. Nothing is inserted in the event queue at all. foo() is executed and then when that function returns, the console.log('bar') is executed.
So process.nextTick() is used to explicitly schedule something to run later after the current thread of JS execution has finished.
It's similar to setTimeout(), but process.nextTick() runs as soon as possible after the current thread of execution whereas setTimeout() can be set with a specific time delay.
It appears your code example is taken from this article: Understanding process.nextTick(). There is a lot more explanation in that article.
Another reference: What are the proper use cases for process.nextTick in Node.js?
I have a heavy data processing operation that I need to get done per 10-12 simulatenous request. I have read that for higher level of concurrency Node.js is a good platform and it achieves it by having an non blocking event loop.
What I know is that for having things like querying a database, I can spawn off an event to a separate process (like mongod, mysqld) and then have a callback which will handle the result from that process. Fair enough.
But what if I want to have a heavy piece of computation to be done within a callback. Won't it block other request until the code in that callback is executed completely. For example I want to process an high resolution image and code I have is in Javascript itself (no separate process to do image processing).
The way I think of implementing is like
get_image_from_db(image_id, callback(imageBitMap) {
heavy_operation(imageBitMap); // Can take 5 seconds.
});
Will that heavy_operation stop node from taking in any request for those 5 seconds. Or am I thinking the wrong way to do such task. Please guide, I am JS newbie.
UPDATE
Or can it be like I could process partial image and make the event loop go back to take in other callbacks and return to processing that partial image. (something like prioritising events).
Yes it will block it, as the callback functions are executed in the main loop. It is only the asynchronously called functions which do not block the loop. It is my understanding that if you want the image processing to execute asynchronously, you will have to use a separate processes to do it.
Note that you can write your own asynchronous process to handle it. To start you could read the answers to How to write asynchronous functions for Node.js.
UPDATE
how do i create a non-blocking asynchronous function in node.js? may also be worth reading. This question is actually referenced in the other one I linked, but I thought I'd include it here to for simplicity.
Unfortunately, I don't yet have enough reputation points to comment on Nick's answer, but have you looked into Node's cluster API? It's currently still experimental, but it would allow you to spawn multiple threads.
When a heavy piece of computation is done in the callback, the event loop would be blocked until the computation is done. That means the callback will block the event loop for the 5 seconds.
My solution
It's possible to use a generator function to yield back control to the event loop. I will use a while loop that will run for 3 seconds to act as a long running callback.
Without a Generator function
let start = Date.now();
setInterval(() => console.log('resumed'), 500);
function loop() {
while ((Date.now() - start) < 3000) { //while the difference between Date.now() and start is less than 3 seconds
console.log('blocked')
}
}
loop();
The output would be:
// blocked
// blocked
//
// ... would not return to the event loop while the loop is running
//
// blocked
//...when the loop is over then the setInterval kicks in
// resumed
// resumed
With a Generator function
let gen;
let start = Date.now();
setInterval(() => console.log('resumed'), 500);
function *loop() {
while ((Date.now() - start) < 3000) { //while the difference between Date.now() and start is less than 3 seconds
console.log(yield output())
}
}
function output() {
setTimeout(() => gen.next('blocked'), 500)
}
gen = loop();
gen.next();
The output is:
// resumed
// blocked
//...returns control back to the event loop while though the loop is still running
// resumed
// blocked
//...end of the loop
// resumed
// resumed
// resumed
Using javascript generators can help run heavy computational functions that would yield back control to the event loop while it's still computing.
To know more about the event loop visit
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/function*
https://davidwalsh.name/es6-generators