What is Asynchronous Code Execution ?
Last Updated :
07 Feb, 2024
Asynchronous code execution refers to executing code where some tasks are executed out of order. In asynchronous code, operations don’t block subsequent code execution. Instead, tasks that take time to complete, such as I/O operations, network requests, or timers, are scheduled to run separately from the main execution flow. Asynchronous operations typically use callbacks, promises, or async/await syntax in JavaScript to handle the completion of tasks and execute code once the tasks are finished.
Example: Here, setTimeout()
is an asynchronous operation that schedules the execution of its callback function after a specified delay (1 second). While waiting for the timeout to complete, the program continues executing the next line (console.log('Third')
) without blocking, demonstrating asynchronous code execution. The output might appear as “First”, “Third”, and then “Second” after a 1-second delay.
Javascript
console.log( 'First' );
setTimeout(() => {
console.log( 'Second' );
}, 1000);
console.log( 'Third' );
|
Output:
First
Third
//after 1 second
Second
Share your thoughts in the comments
Please Login to comment...