This question already has answers here:
JavaScript, Node.js: is Array.forEach asynchronous?
(12 answers)
Closed 5 years ago.
var a = [1,2,3,4,5,6,7,8,9,10];
a.forEach(item => {
...
});
console.log('all done');
Is it ever possible in browser or node environment that it console 'all done' before the forEach completes its execution? If yes then when?
No. Your code is synchronous so it can never happen unless you explicitly execute the .forEach function in an async block.
No. forEach is a synchronous function.
It is possible to call asynchronous functions from within it, and they might not have completed, but the loop itself will have done.
Related
This question already has answers here:
Why the function called by setTimeout has no callstack limit?
(2 answers)
Closed 5 months ago.
Consider this :
function some_function(){
axios.get(...)
.then(x=>{//handle})
.catch(e=>{//handle})
.then(()=>{
setTimeout(()=>{
some_function();
},5000);
}
I don't care aboout cancelation, I just care if this is gonna blow the stack up.
No, it won't cause the stack to grow. setTimeout() callback functions are called from the main event loop asynchronously, not from your function. Your function returns immediately after you make the axios.get() call (since that's also asynchronous, it doesn't wait for it, either).
This question already has answers here:
Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference
(7 answers)
How do I return the response from an asynchronous call?
(41 answers)
Closed 4 years ago.
I feel something does not fit in my picture, when I try to wrap my head around async-programming. Every JavaScript tutorial says something like:
Every time an expensive operation occurs, we pass a callback function that will be called once we can continue with the processing. We’re not waiting for that to finish before going on with the rest of the program.
How I see it, when we execute some command, we need the result of this code for next command. So, how could async call not to block execution, when we need the result of the async call in rest of our program?
For example such simple code:
var a = 12;
var c = 0;
function sum( data ) {
console.log( data.b + a );
c = data.b + a // global c here
console.log( 'and now we can continue with next command' );
}
verySlowAndExpensiveAsyncCall( a, sum );
console.log( 'can we execute this without waiting previous to finish?' );
console.log( c );
For my procedural mind I feel I can't execute any next command anyway, because I need the value of c to continue. So where is the non-blocking nature here?
My question is maybe not strictly Stack Overflow-questions, but it is fundametal to understand async-programming and I have read a lot of docs and somehow missed this important part: why and when (on which conditions) asynchronous code is non-blocking?
I expect either explanation or source of the explanation as answer.
This question already has answers here:
How do you work with an array of jQuery Deferreds? [duplicate]
(4 answers)
Closed 6 years ago.
I have a page like this:
$(document).ready(function() {
$.when(ajaxcall1(),ajaxcall2(),ajaxcall3()).done(finalCall());
});
For some reason the finalCall() is firing simultaneous to the three calls surrounded by $.when().
I tried calling a reference to the finalCall() function as in:
$(document).ready(function() {
$.when(ajaxcall1(),ajaxcall2(),ajaxcall3()).done(finalCall);
});
But even then, it still fires the function before the prior 3 complete.
NOTE: I am not including the functions here as they are not relevant. I just need to know why the finalCall() function would fire simultaneous to then $.when() functions.
Thank you.
$.when does not call your callback at all. You are doing it yourself:
// vv
$.when(ajaxcall1(),ajaxcall2(),ajaxcall3()).done(finalCall());
// ^^
Change that to
$.when(ajaxcall1(),ajaxcall2(),ajaxcall3()).then(finalCall);
where the function is actually passed into the promise method, and it'll work (assuming your ajax functions return promises).
This question already has answers here:
Are all javascript callbacks asynchronous? If not, how do I know which are?
(5 answers)
Closed 7 years ago.
I am a little confusing about the async function and sync function, how can I determine if it is an async function?
My assumption is all the function which accept a callback is non-block and async, but here is a exception I found:
I found the Array.prototype.forEach is a block function even if it accept a callback as a parameter.
function test(){
[1,2,3,4,5].forEach(function(item){
for(var i =0; i<100000; i++){
console.log('test');
}
});
console.log('end');
}
test();
this function will continue to print test until all the callback finish, it won't return at once to run console.log('end')
really confusing, how can I determine if a function will return at once?
You can use the common sense rule here, is there any need for async in your code? Why do we need to delay the function? All of the array items are available for you, even if they were not, there should be another reader for example which is async then populate the array with avaialble items and then iterate over them. If you need async you will notice this for sure.
This question already has answers here:
Why is setTimeout(fn, 0) sometimes useful?
(19 answers)
Closed 9 years ago.
I have seen some JavaScript code that has the following:
var myFunc = function () {
timeout = setTimeout(myFunc, 0);
}
It seems that this would immediately recall the function.
What use case is there for this?
Read this.
In short, it "pauses" the JavaScript execution to let the rendering threads catch up. It gives the browser a chance to finish doing some none-JavaScript things that have been waiting to finish before attending to this new piece of JavaScript.