Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST

Basic JavaScript

JS Tutorial JS Syntax JS Variables JS Operators JS If Conditions JS Loops JS Strings JS Numbers JS Functions JS Objects JS Scope JS Dates JS Temporal Dates JS Arrays JS Sets JS Maps JS Iterations JS Math JS RegExp JS Data Types JS Errors JS Debugging JS Conventions JS References JS ECMAScript 2026 JS Versions

JS HTML

JS HTML DOM JS Events JS Projects

JS Advanced

JS Functions JS Objects JS Classes JS Async JS Modules JS Meta & Proxy JS Typed Arrays JS DOM Navigation JS Windows JS Web APIs JS AJAX JS JSON JS jQuery JS Graphics JS Examples JS Reference


JS Asynchronous Programming

Control Flow is the order in which statements are executed in a program.

By default, JavaScript runs code from top to bottom and left to right.

Asynchronous programming changes when some code runs.


How JavaScript Runs Code

JavaScript executes code one line at a time.

Each line must finish before the next line runs.

Example:

console.log("A");
console.log("B");
console.log("C");

The output is always A, B, C.

This is called synchronous code.


The Problem With Blocking Code

Some tasks take time.

Examples include network requests, timers, and user input.

If JavaScript waited for these tasks, the page would freeze.

A frozen page is a broken page.


Why JavaScript Uses Async

JavaScript uses asynchronous code to stay responsive.

Long tasks are started and finished later.

The rest of the program continues running.

Example:

console.log("Start");
setTimeout(function() {
  console.log("Later");
}, 1000);
console.log("End");

Note

Understanding async is about understanding time.

The output is Start, End, Later.

Async code does not block execution.


What Asynchronous Means

Asynchronous code runs after the current code finishes.

It does not run immediately.

JavaScript registers the task and moves on.

  • Timers run later.
  • Events run when triggered.
  • Network requests run when data arrives.

A Simple Timeline

Think of async code as a to-do list.

JavaScript finishes what it is doing first.

When the task is ready, JavaScript comes back to it.

Async does not mean parallel.

It means deferred.


Common Beginner Confusion

Beginners expect async results immediately.

Example mistake:

let result;
setTimeout(function() {
  result = 5;
}, 1000);
console.log(result);

The value is undefined.

The async code has not finished yet.


The Async Building Blocks

JavaScript handles async using a few core tools.

  • Callbacks
  • Promises
  • async and await

Each tool exists to solve problems from the previous one.


What You Will Learn Next

This tutorial builds async step by step.

  • Why callbacks were created.
  • How promises represent future values.
  • Why async and await are preferred.
  • How to debug async code.

You do not need to memorize anything yet.

You need to understand the flow.


Next: JavaScript Callbacks

The first async solution in JavaScript was callbacks.

The next page explains what they are and why they caused problems.




×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.

-->