Asynchronous JavaScript: Why and How
Seoul National University of Science and Technology
Information Technology Management
Lecture slides index
March 28, 2026
Synchronous
Asynchronous

Asynchronous programming allows JavaScript to handle multiple tasks without blocking the user interface
Synchronous system

Asynchronous system

Problem:

setTimeout runs later, even with 0 msNote
Callbacks pass results from one step to another
| Name | Description |
|---|---|
setTimeout(callBack, delay) |
Runs callBack once after a delay in ms, returns timer id |
setInterval(callBack, delay) |
Runs callback repeatedly every delay in ms, returns timer id |
clearTimeout(timerID) |
Stop the given timer |
clearInterval(timerID) |
Stop the given interval |
setTimeout and setInterval return an ID representing the timer.
clearTimeout/clearInterval functionssetTimeout exampledocument.addEventListener('DOMContentLoaded', () => {
document.getElementById("demo-btn").addEventListener("click", delayedMessage);
});
function delayedMessage() {
document.getElementById("output-text").textContent = "Wait for it...";
setTimeout(showMessage, 3000);
}
function showMessage() {
document.getElementById("output-text").textContent = "Done!";
}setInterval examplelet timerId = null;
document.getElementById("start-btn").addEventListener("click", startTimer);
document.getElementById("stop-btn").addEventListener("click", stopTimer);
function startTimer() {
timerId = setInterval(() => {
document.getElementById("timer-text").textContent += "⏱️ ";
}, 1000);
}
function stopTimer() {
clearInterval(timerId);
}We need a better way to manage asynchronous code!
Promises, Async/Await, and Fetch

data-attributedataset object
data- (see the example)HTML
JavaScript

Web Programming