Asynchronous Code

Do Not Wait

Asynchronous code starts a task (timer, network request, file read) and continues immediately. The result is handled later by a callback, promise or await.

Example

Notice that "End" prints before the timeout, even with a 0 ms delay.

console.log('Start');
setTimeout(() => console.log('Timeout finished'), 0);
console.log('End');

Output

Start
End
Timeout finished

Sync vs Async

SynchronousAsynchronous
Executionin order, one after anotherstarts now, finishes later
Blocks the threadyesno
Examplesloops, math, array methodsfetch, setTimeout, file/DB access
Handled withnormal codecallbacks, promises, async/await