惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
小众软件
小众软件
Engineering at Meta
Engineering at Meta
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
G
Google Developers Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
V
V2EX
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Google DeepMind News
Google DeepMind News
Security Archives - TechRepublic
Security Archives - TechRepublic
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
罗磊的独立博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MongoDB | Blog
MongoDB | Blog
L
LINUX DO - 热门话题
酷 壳 – CoolShell
酷 壳 – CoolShell
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
H
Help Net Security
Martin Fowler
Martin Fowler
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - Franky
V
Vulnerabilities – Threatpost
云风的 BLOG
云风的 BLOG
博客园_首页
C
Cybersecurity and Infrastructure Security Agency CISA
量子位
Stack Overflow Blog
Stack Overflow Blog
Recent Announcements
Recent Announcements
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
I
Intezer
Scott Helme
Scott Helme
A
About on SuperTechFans
博客园 - 司徒正美
Hacker News: Ask HN
Hacker News: Ask HN
The GitHub Blog
The GitHub Blog
Forbes - Security
Forbes - Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
博客园 - 聂微东
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Spread Privacy
Spread Privacy
T
Tailwind CSS Blog
S
Security Affairs
宝玉的分享
宝玉的分享

Dmitri Pavlutin Blog

Pure Functions in JavaScript: A Beginner's Guide Record Type in TypeScript: A Quick Intro How to Write Comments in React: The Good, the Bad and the Ugly 4 Ways to Create an Enum in JavaScript React forwardRef(): How to Pass Refs to Child Components TypeScript Function Types: A Beginner's Guide How to Use v-model to Access Input Values in Vue Mastering Vue refs: From Zero to Hero Environment Variables in JavaScript: process.env 5 Must-Know Differences Between ref() and reactive() in Vue How to Destructure Props in Vue (Composition API) Triangulation in Test-Driven Development How to Use nextTick() in Vue Programming to Interface Vs to Implementation A Smarter JavaScript Mapper: array.flatMap() Array Grouping in JavaScript: Object.groupBy() How to Access ES Module Metadata using import.meta JSON Modules in JavaScript How to Trim Strings in JavaScript TypeScript Function Overloading How to Debounce and Throttle Callbacks in Vue How to Show/Hide Elements in Vue Sparse vs Dense Arrays in JavaScript How to Fill an Array with Initial Values in JavaScript Covariance and Contravariance in TypeScript What are Higher-Order Functions in JavaScript? How to Use TypeScript with React Components Index Signatures in TypeScript How to Use React useReducer() Hook unknown vs any in TypeScript A Guide to React Context and useContext() Hook How to Use Promise.any() 2 Ways to Remove a Property from an Object in JavaScript 'return await promise' vs 'return promise' in JavaScript How to Use Promise.allSettled() How to Use fetch() with JSON JavaScript Promises: then(f,f) vs then(f).catch(f) What is a Promise in JavaScript? How to Use Promise.all() A Simple Guide to Component Props in React Don't Stop Me Now: How to Use React useTransition() hook A Simple Explanation of JavaScript Variables: const, let, var ES Modules Dynamic Import How to Memoize with React.useMemo() How to Cleanup Async Effects in React Why Math.max() Without Arguments Returns -Infinity How to Debounce and Throttle Callbacks in React Don't Confuse Function Expressions and Function Declarations in JavaScript How to Use ES Modules in Node.js Solving a Mystery Behavior of parseInt() in JavaScript How to Use Array Reduce Method in JavaScript 3 Ways to Merge Arrays in JavaScript A Guide to Jotai: the Minimalist React State Management Library The Difference Between Values and References in JavaScript How to Implement a Queue in JavaScript A Helpful Algorithm to Determine "this" value in JavaScript React useRef() Hook Explained in 3 Steps 7 Interview Questions on "this" keyword in JavaScript. Can You Answer Them? How to Greatly Enhance fetch() with the Decorator Pattern 7 Interview Questions on JavaScript Closures. Can You Answer Them? What's a Method in JavaScript? array.sort() Does Not Simply Sort Numbers in JavaScript How to Solve the Infinite Loop of React.useEffect() The New Array Method You'll Enjoy: array.at(index) What's the Difference between DOM Node and Element? Why Promises Are Faster Than setTimeout()? Everything About Callback Functions in JavaScript How React Updates State 5 Mistakes to Avoid When Using React Hooks 5 Best Practices to Write Quality JavaScript Variables Type checking in JavaScript: typeof and instanceof operators 3 Ways to Check if a Variable is Defined in JavaScript React Forms Tutorial: Access Input Values, Validate, Submit Forms Prototypal Inheritance in JavaScript How to Timeout a fetch() Request How to Learn JavaScript If You're a Beginner A Simple Explanation of React.useEffect() A Simple Explanation of JavaScript Iterators How to Use React Controlled Inputs Everything about null in JavaScript How to Use Fetch with async/await Getting Started with Arrow Functions in JavaScript Front-end Architecture: Stable and Volatile Dependencies Is it Safe to Compare JavaScript Strings? How to Access Object's Keys, Values, and Entries in JavaScript What Actually is a String in JavaScript? 3 Ways to Shallow Clone Objects in JavaScript (w/ bonuses) Checking if an Array Contains a Value in JavaScript JavaScript Event Delegation: A Beginner's Guide How to Parse URL in JavaScript: hostname, pathname, query, hash 3 Ways to Detect an Array in JavaScript How to Get the Screen, Window, and Web Page Sizes in JavaScript 3 Ways to Check If an Object Has a Property/Key in JavaScript How to Compare Objects in JavaScript Object.is() vs Strict Equality Operator in JavaScript Own and Inherited Properties in JavaScript 5 Differences Between Arrow and Regular Functions How to Use Object Destructuring in JavaScript Your Guide to React.useCallback() 5 JavaScript Scope Gotchas
An Interesting Explanation of async/await in JavaScript
Dmitri Pavlutin · 2020-09-01 · via Dmitri Pavlutin Blog

In JavaScript, you can code async tasks in 3 ways.

The first approach is using callbacks. When an async operation had been completed, a callback function (meaning call me back when the operation has been completed) is executed:


const callbackFunction = result = {

// Called when the operation completes

};

asyncOperation(params, callbackFunction);


But as soon as you handle multiple async operations, the callback functions nest into each other ending in callback hell.

A promise is a placeholder object for the results of an async task. With the use of promises, you can handle the async operations easier.


const promise = asyncOperation(params);

promise.then(result => {

// Called when the operation completes

});


Have you seen the .then().then()...then() chains of promises 🚂🚃🚃🚃🚃? An issue of promises is their verbosity.

Finally, the third attempt is the async/await syntax (starting ES2017). It lets you write async code in a concise and sync manner:


(async function() {

const result = await asyncOperation(params);

// Called when the operation completes

})();


In this post I'm going to explain, step by step, how to use async/await in JavaScript.

Table of Contents

  • 1. The sync addition
  • 2. The async addition
  • 3. The broken async addition
  • 4. Nesting async functions
  • 5. Parallel async
  • 6. Practical async example
  • 7. Conclusion

1. The sync addition

Because the title of the post mentions an interesting explanation, I'm going to gradually explain async/await in regards to a greedy boss story.

Let's start with a simple (synchronous) function which task is to calculate the salary increase:


function increaseSalary(base, increase) {

const newSalary = base + increase;

console.log(`New salary: ${newSalary}`);

return newSalary;

}

increaseSalary(1000, 200); // => 1200

// logs "New salary: 1200"


increaseSalary() is a function that sums 2 numbers. n1 + n2 is a synchronous operation.

The boss doesn't want a quick increase in the employee's salary ☹. So you're not allowed to use the addition operator + in increaseSalary() function.

Instead, you have to use a slow function that requires 2 seconds to summarize numbers. Let's name the function slowAddition():


function slowAddition(n1, n2) {

return new Promise(resolve => {

setTimeout(() => resolve(n1 + n2), 2000);

});

}

slowAddition(1, 5).then(sum => console.log(sum));

// After 2 seconds logs "6"


slowAddition() returns a promise, which resolves to the sum of arguments after a delay of 2 seconds.

How to update the increaseSalary() function to support the slow addition?

2. The async addition

The first naive attemtp is to replace the n1 + n2 expression with a call to slowAddition(n1, n2):


function increaseSalary(base, increase) {

const newSalary = slowAddition(base, increase);

console.log(`New salary: ${newSalary}`);

return newSalary;

}

increaseSalary(1000, 100); // => [object Promise]

// Logs "New salary [object Promise]"


Unfortunately, the function increaseSalary() doesn't know how to handle promises. The function considers promises regular objects: it doesn't know how and when to extract values from promises.

Now it's the right time to make the increaseSalary() aware of how to handle the promise returned by slowAddition() using async/aware syntax.

First, you need to add the async keyword near the function declaration. Then, inside the function body, you need to use the await operator to make the function wait for the promise to be resolved.

Let's make these changes to increaseSalary() function:


async function increaseSalary(base, increase) {

const newSalary = await slowAddition(base, increase);

console.log(`New salary: ${newSalary}`);

return newSalary;

}

increaseSalary(1000, 200); // => [object Promise]

// After 2 seconds logs "New salary 1200"


JavaScript evaluates const newSalary = await slowAddition(base, increase) the following way:

  1. await slowAddition(base, increase) pauses the increaseSalary() function execution
  • After 2 seconds, the promise returned by slowAddition(base, increase) is resolved
  • increaseSalary() function execution resumes
  • newSalary is assigned with the promise's resolved value 1200 (1000+200)
  • The function execution continues as usual.

In simple words, when JavaScript encounters await promise in an async function, it pauses the function execution until the promise is resolved. The promise's resolved value becomes the result of await promise evaluation.

Even though return newSalary returns the number 1200, if you look at the actual value returned by the function increaseSalary(1000, 200) — it is still a promise!

An async function always returns a promise, which resolves to the value of return value inside the function body:


increaseSalary(1000, 200).then(salary => {

salary; // => 1200

});


async functions returning promises is a good thing because you can nest async functions.

3. The broken async addition

It's unfair that the boss has put a requirement to increase slowly the salary. You've decided to sabotage the slowAddition() function.

You modify the slow addition function to reject the numbers addition:


function slowAdditionBroken(n1, n2) {

return new Promise((resolve, reject) => {

setTimeout(() => reject(new Error('Unable to sum numbers')), 3000);

});

}

slowAdditionBroken(1, 5).catch(e => console.log(e.message));

// After 3 seconds logs "Unable to sum numbers"


How to handle a rejected promise inside the calculateSalary() async function? Just wrap the await operator in an try/catch clause:


async function increaseSalaryBroken(base, increase) {

let newSalary;

try {

newSalary = await slowAdditionBroken(base, increase);

} catch (e) {

console.log(`Error: ${e.message}`);

newSalary = base * 2;

}

console.log(`New salary: ${newSalary}`);

return newSalary;

}

increaseSalaryBroken(1000, 200);

// After 3 seconds logs

// "Error: Unable to sum numbers", "New salary: 2000"


At the expression await slowAdditionBroken(base, increase) JavaScript pauses the function execution and waits until the promise is fulfilled (the promise successfully resolved) or rejected (an error has occurred).

After 3 seconds, the promise is rejected with new Error('Unable to sum numbers'). Because of rejection, the function execution jumps into the catch (e){ } clause where the base salary is multiplied by 2.

Miser pays twice. Now the boss has to pay double salaries. Nice one!

If you don't catch a rejected promise, then the error propagates and the promise returned by the async function gets rejected:


async function increaseSalaryBroken(base, increase) {

const newSalary = await slowAdditionBroken(base, increase);

return newSalary;

}

increaseSalaryBroken(1000, 200).catch(e => {

e.message; // => "Unable to sum numbers"

});


4. Nesting async functions

Despite return <value> expression inside an async function returning the payload value and not a promise, still, when the async function is invoked it returns a promise.

That's a good thing because you can nest asynchronous functions!

For example, let's write an async function that increases an array of salaries using the slowAddition() function:


async function increaseSalaries(baseSalaries, increase) {

let newSalaries = [];

for (let baseSalary of baseSalaries) {

newSalaries.push(

await increaseSalary(baseSalary, increase);

);

}

console.log(`New salaries: ${newSalaries}`);

return newSalaries;

}

increaseSalaries([950, 800, 1000], 100);

// After 6 seconds logs "New salaries: 1050,900,1100"


await increaseSalary(baseSalary, increase) is called 3 times for each salary in the array. Each time JavaScript waits 2 seconds until the sum is calculated.

This way you can nest async function into async functions.

5. Parallel async

In the previous example of summing an array of salaries, the summing happens in sequence: the function is paused 2 seconds for every salary.

But you can make salary increases in parallel! Let's use Promise.all() utility function to start all the salary increases simultaniously:


async function increaseSalaries(baseSalaries, increase) {

let salariesPromises = [];

for (let baseSalary of baseSalaries) {

salariesPromises.push(

increaseSalary(baseSalary, increase)

);

}

const newSalaries = await Promise.all(salariesPromises);

console.log(`New salaries: ${newSalaries}`);

return newSalaries;

}

increaseSalaries([950, 800, 1000], 100);

// After 2 seconds logs "New salaries: 1050,900,1100"


The salary increase tasks start right away (await isn't used near increaseSalary(baseSalary, increase)) and promises are collected in salariesPromises.

await Promise.all(salariesPromises) then pauses the function execution until all the async operations processed in parallel finish. Finally, only after 2 seconds, newSalaries variable contains the increased salaries.

You've managed to increase the salaries of all employees in just 2 seconds, even if each operation is slow and requires 2 seconds. You've tricked the boss again!

6. Practical async example

A common situation when you'd want to use async/await syntax is to fetch remote data.

fetch() method is a good candidate to be used with async/await because it returns a promise that resolves to the value returned by a remote API.

For example, here's how you would fetch a list of movies from a remote server:


async function fetchMovies() {

const response = await fetch('https://api.example.com/movies');

if (!response.ok) {

throw new Error('Failed to fetch movies');

}

const movies = await response.json();

return movies;

}


await fetch('https://api.example.com/movies') is going to pause fetchMovies() execution until the request is completed. Then you can extract the actual using await response.json().

7. Conclusion

async/await is syntactic sugar on top of the promises and provides a way to handle the asynchronous tasks in a synchronous manner.

async/await has 4 simple rules:

  1. A function handling an asynchronous task must be marked using the async keyword.
  2. await promise operator pauses the function execution until promise is either resolved successfully or rejected.
  3. If promise resolves successfully, the await operator returns the resolved value: const resolvedValue = await promise. Otherwise, you can catch a rejected promise inside try/catch.
  4. An async function always returns a promise, which gives the ability to nest async functions.

Having a good understanding of async/await, check my detailed post on How to Use Fetch with async/await.

Quiz: Is it an error to await for primitive values, e.g. await 3?