sequenceDiagram
participant Client
participant Server
Client->>+Server: Request
Note right of Server: Processing Request...
Server-->>-Client: Response
Promises, async/await and fetch
Seoul National University of Science and Technology
Information Technology Management
Lecture slides index
March 31, 2026
Solution:

Promise is an object that represents the result of an asynchronous operationPromise has three states:
then method is employed, while the catch method is used to address the rejection of the promise.
Promise (.then() and .catch()).then() to handle the result when a Promise is fulfilled.catch() to handle errors when a Promise is rejected.then() receives the result from the previous stepPromise → the next .then() waits for it.catch() at the end of a chain captures any rejection or error thrown from any preceding .then() blockPromises are a great way to deal with the limitations that callbacks introduce when we need to perform multiple asynchronous operations that should be executed in a consecutive order.
Promises will handle errors more easily, so the readability of the code should be clearer and easier to maintain in long term.
readFile("docs.md") // returns a Promise
.then(convertMarkdownToHTML) // gets the content of docs.md
.then(addCssStyles) // gets the HTML from the previous step
.then(docs => saveFile(docs, "docs.html")) // saves the final version
.then(ftp.sync) // syncs to the server
.then(result => {
// do something with result
})
.catch(error => console.log(error));then(...) receives the resolved value from the previous Promise, and passes its own result to the next .then().
async and await provide a cleaner way to work with promises (introduced in ES2017)async is used to define a function that returns a Promiseawait pauses execution until the Promise is resolvedasync keywordasync keyword is used to define an asynchronous functionasync function always returns a PromisePromise that resolves to “Hello”await keywordawait keyword is used inside an async functionPromise is resolvedPromise.then() vs async/awaitPromises.then()
async/await makes asynchronous code easier to readtry/catchasync/await, we handle errors with try/catch
try blockcatch block.catch() in promise chainsfetch()?fetch() is a built-in browser function used to request data from a serverfetch() returns a PromiseBasic usage
fetch() starts a request and returns a Promise that will contain the responsefetch() with .then()fetch() returns a Promise.then() to process the response
Note
response.json() also returns a Promise
response.json()?fetch() is a Response object, not the actual dataImportant
response.json() reads the response bodyPromisesequenceDiagram
participant Client
participant Server
Client->>+Server: Request
Note right of Server: Processing Request...
Server-->>-Client: Response
fetch() with async/awaitasync/await instead of .then() for better readabilityNote
Each await waits for a Promise to resolve

fetch()response.json()Flow
fetch() → Promiseresponse.json() → Promisedata → JavaScript objectfetch() to get data

Web Programming