Asynchronous TypeScript
Ketan Mayer-Patel
UNC Department of Computer Science
COMP 426: Modern Web Programming (Fall 2026)
Module 1: Fundamentals
Lecture 06
Announcements
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall ‘26)
M1: Fundamentals
L06
!
Recap: Client and Server Interactions
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall ‘26)
M1: Fundamentals
L06
Requests resource A
Responds w/ resource A
Server
Client
API Endpoints
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall ‘26)
M1: Fundamentals
L06
API Endpoints
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
M1: Fundamentals
L06
JSON
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
M1: Fundamentals
L06
Fetching Data from a Server
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
M1: Fundamentals
L06
const data = fetch(url).json();
Fetching Data from a Server
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
M1: Fundamentals
L06
HTTP and APIs
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
Output:
Start
M1: Fundamentals
L06
script.ts
console.log(“Start”);�const data = fetch(`url`).json();
console.log(data);
console.log(“End”);
This can take a (really) long time!
Remember TS is single-threaded
Our program is blocked from doing anything else while this runs!
Motivations for Async Programming
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
M1: Fundamentals
L06
Solution: Asynchronous Event Model
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
M1: Fundamentals
L06
Asynchronous Programming With Callbacks
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
Promises
script.ts
fetch("http://some/api/endpoint")
.then((result) => {� // Handle case when a value is
// successfully produced.
return fetch(http://some/other/endpoint);
})
.then((result) => {
// Handle case when a value is
// successfully produced.
})
.catch((error) => {� // Handle case where an error occurs
// along the way.
console.error(error);� })�
M1: Fundamentals
L06
async / await
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
With a partner or small group:
Discuss for ~1-2 minutes - what do you think the output of this script be?
M1: Fundamentals
L06
script.ts
console.log(“Start”);
// Mark the “expensive” operation
// with async so that the program
// knows to kick this off and move
// on (handle the result later).
// This prevents blocking!!
const performExpOp = async () => {
await expensiveOperation();
return "I love COMP 426!";
}
// Kick off the expensive operation.
const result = performExpOp();
// Continue running while we wait.�console.log(“End”);
console.log(result);
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
Output:
Start
End
Promise { <pending> }
M1: Fundamentals
L06
script.ts
console.log(“Start”);
// Mark the “expensive” operation
// with async so that the program
// knows to kick this off and move
// on (handle the result later).
// This prevents blocking!!
const performExpOp = async () => {
await expensiveOperation();
return "I love COMP 426!";
}
// Kick off the expensive operation.
const result = performExpOp();
// Continue running while we wait.�console.log(“End”);
console.log(result);
Not �“I love COMP 426!”
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
M1: Fundamentals
L06
script.ts
console.log(“Start”)
Execution Sequence
performExpOp() �is called.
Async process kicked off
Async Process
console.log(“End”)
Async process completes and returns
Some time later...
console.log(“Start”);
// Mark the “expensive” operation
// with async so that the program
// knows to kick this off and move
// on (handle the result later).
// This prevents blocking!!
const performExpOp = async () => {
await expensiveOperation();
return "I love COMP 426!";
}
// Kick off the expensive operation.
const result = performExpOp();
// Continue running while we wait.�console.log(“End”);
console.log(result);
console.log(result)
The result’s intended value is not found until here.
We can only do something with the intended value after it is found.
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
M1: Fundamentals
L06
script.ts
console.log(“Start”)
Execution Sequence
performExpOp() �is called.
Async process kicked off
Async Process
console.log(“End”)
Async process completes and returns
Some time later...
Promise<T> objects have a then method that takes in a function of type (result: T) => void as a parameter.
Once the asynchronous process completes, the Promise calls this function and passes in the result as the parameter!
This allows us to do something with the result of an asynchronous function.
console.log(“Start”);
// Mark the “expensive” operation
// with async so that the program
// knows to kick this off and move
// on (handle the result later).
// This prevents blocking!!
const performExpOp = async () => {
await expensiveOperation();
return "I love COMP 426!";
}
// Kick off the expensive operation.
performExpOp()
.then((result) => {
console.log(result);
});
// Continue running while we wait.�console.log(“End”);
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
M1: Fundamentals
L06
script.ts
console.log(“Start”)
Execution Sequence
performExpOp() �is called.
Async process kicked off
Async Process
console.log(“End”)
Async process completes and returns
The function passed into then is called.
console.log(result)
Some time later...
console.log(“Start”);
// Mark the “expensive” operation
// with async so that the program
// knows to kick this off and move
// on (handle the result later).
// This prevents blocking!!
const performExpOp = async () => {
await expensiveOperation();
return "I love COMP 426!";
}
// Kick off the expensive operation.
performExpOp()
.then((result) => {
console.log(result);
});
// Continue running while we wait.�console.log(“End”);
i.e., the passed in function is not added to the call stack until after the async process completes.
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
Output:
Start
End
I love COMP 426!
M1: Fundamentals
L06
script.ts
console.log(“Start”)
Execution Sequence
performExpOp() �is called.
Async process kicked off
Async Process
console.log(“End”)
Async process completes and returns
The function passed into then is called.
console.log(result)
Some time later...
console.log(“Start”);
// Mark the “expensive” operation
// with async so that the program
// knows to kick this off and move
// on (handle the result later).
// This prevents blocking!!
const performExpOp = async () => {
await expensiveOperation();
return "I love COMP 426!";
}
// Kick off the expensive operation.
performExpOp()
.then((result) => {
console.log(result);
});
// Continue running while we wait.�console.log(“End”);
i.e., the passed in function is not added to the call stack until after the async process completes.
With a partner or small group:
What should the output of this code be?
Let’s spend ~2-3 minutes on this!
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
Practice
script.ts
const cat = async () => {� await // Action takes 500ms to perform.
return ...�}�const dog = async () => {
await // Action takes 100ms to perform.
return ...
}
console.log(“One”);�
cat()� .then(result => { console.log(“Cat”) });
console.log(“Two”);
dog()� .then(result => { console.log(“Dog”) });
console.log(“Three”);
M1: Fundamentals
L06
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
M1: Fundamentals
L06
script.ts
const cat = async () => {� await // Action takes 500ms to perform.
return ...�}�const dog = async () => {
await // Action takes 100ms to perform.
return ...
}
console.log(“One”);
cat()� .then((result) => {console.log(“Cat”)});
console.log(“Two”);
dog()� .then((result) => {console.log(“Dog”)});
console.log(“Three”);
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
Attendance Question!
What should the output of this code be?
script.ts
const foo = async () => {� await // Action runs for 100ms
return async () => {
return await // Action runs for 300ms
}�}
const bar = async () => {
return await // Action runs for 200ms�}
bar().then(_=> { console.log(“bar returned!”); })
foo().then(result => {� result().then(_ => {� console.log(“BOO!”);� })� console.log(“foo returned!”);�})
M1: Fundamentals
L06
!
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
M1: Fundamentals
L06
script.ts
const foo = async () => {� await // Action runs for 100ms
return async () => {
await // Action runs for 300ms and returns� }�}
const bar = async () => {
await // Action runs for 200ms and returns�}
bar().then(_=> { console.log(“bar returned!”); })
foo().then(res => {� res().then(_ => {� console.log(“BOO!”);� })� console.log(“foo returned!”);�})
Recall: Fetching Data from a Server
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
const result = await fetch(...);
const json = await result.json(...);
M1: Fundamentals
L06
With a partner or small group:
Your Turn!
Asynchronous TypeScript
COMP 426: Modern Web Programming (Fall '26)
M1: Fundamentals
L06