Synchronous code runs in sequence. This means that each operation must wait for the previous one to complete before executing.
console.log('One');
console.log('Two');
console.log('Three');
// LOGS: 'One', 'Two', 'Three'
Asynchronous code runs in parallel. This means that an operation can occur while another one is still being processed.
console.log('One');
setTimeout(() => console.log('Two'), 100);
console.log('Three');
// LOGS: 'One', 'Three', 'Two'
Asynchronous code execution is often preferable in situations where execution can be blocked indefinitely. Some examples of this are network requests, long-running calculations, file system operations etc. Using asynchronous code in the browser ensures the page remains responsive and the user experience is mostly unaffected.
Would you like to help us improve 30 seconds of code?Take a quick survey
Snippet collection
Prepare for your next JavaScript interview with this collection of interview questions and answers.
JavaScript, Function
Performs left-to-right function composition for asynchronous functions.
JavaScript, Function
Delays the execution of an asynchronous function.
JavaScript, Function
Converts an asynchronous function to return a promise.