JavaScript is a single threaded language, which can only run one statement of code at at a time.
The JavaScript runtime can only do one thing at a time. In order to write performant, responsive code we utilize callbacks in order to perform slow tasks asynchronously. This keeps the single thread free to run code that’s ready to run now.
The event loop facilitates the management of callbacks and asyncronous behaviour within the single threaded runtime.
Bonus video [25 min]: What the heck is the event loop anyway?
Synchronous code (often called iterative code) is code that runs in linear order, one expression following the next, from beginning to end.
Asynchronous code is code that we schedule to run later. Usually, we have to wait for some other code to complete before we can proceed with our current code, so we wrap it in a callback, and ask JavaScript to return asynchronously to our callback.
and the function
predict the output of asyncy(callback);
function tenIntervals( ) {
let count = 0;
let handler = setInterval(function() {
count++;
console.log('execute');
if (count == 10) {
clearInterval(handler);
}
}, 1000);
}
tenIntervals();