Course is an introduction to

In the world of software development, clean code => easy to read code => easy to maintain code, and the async/await feature in ES2017 allows us to write code that is more intuitive and easier to understand than callback hell and Promise chain calls. The await keyword receives a Promise and waits for the code to be executed until the Promise state becomes resolved or Rejected. This feature allows our asynchronous code to read more like synchronous code.

This course consists of 8 sections and 20 minutes. We will start with writing simple async/await functions and explore all aspects of the async/await practice with examples. For example, how to combine the Promise and await keywords? How to define different types of async functions? How to safely use async/await, i.e. handle errors? How do I make multiple await serial or parallel? How to use await correctly in loops?

Applicable people

  • Basic knowledge of JS and how to run node.js scripts;
  • [Required] Knows asynchronous processing concepts such as Callback and Promise;
  • [Optional] Learn new asynchronous features and write code that is easier to understand and maintain.
  • [Optional] How to make your JS skills more valuable

Table of contents

1. Write the first async/await function

This tutorial teaches you how to rewrite the code that initiates the HTTP request and parses the response in the async/await style. This tutorial teaches you the basic syntax of async/await.

2. Use the async function in the Promise chain

Explores how to seamlessly use async functions in a Promise chain just like any other Promise.

3. Convert any type of function to the async style

Example shows how to write any function async style, including function declaration, function expression, arrow function, class method, object method, etc.

4. Handle errors in async functions

Unlike the.catch() error handling mechanism in promises, in async functions we need to use the try/catch structure to handle errors properly.

5. Properly handle parallel serialization of multiple await operations

Learn to implement multiple await operations serial or parallel by moving the location of the await keyword, and demonstrate the performance benefits of parallelizing multiple asynchronous operations with data.

6. Parallel multiple await operations with promise.all ()

Learn to use deconstruction and promise.all () in ES2015 to parallelize multiple asynchronous operations. This is ideal for situations where multiple requests need to be made simultaneously without compromising code readability.

7. Combine await with any code compatible with.then()

The await operator is not limited to ES2015 promises. It can be used with any code compatible with the.then() method. Learn how to use it with the popular Promise library through examples.

8. Use await correctly in for loops

Understand the performance impact of different ways of writing await in a for loop and the ways to improve it.

Source link

Code repository: wangshijun/course-javascript-async-await

How to run it (make sure you have Node.js installed) :

git clone https://github.com/wangshijun/course-javascript-async-await.git
cd course-javascript-async-await
node xxx.jsCopy the code

The xxx.js above corresponds to the source code of each course, listed as follows:

  1. Write the first async/await function
  2. Use the async function in the Promise chain
  3. Convert any type of function to the Async style
  4. Handling errors in async functions
  5. Handle parallel serialization of multiple await operations correctly
  6. Use promise.all () to parallelize multiple await operations
  7. Combine await with any code compatible with.then()
  8. Use await correctly in for loops

Runtime environment

  • Node. Js v8.6.0
  • VSCode v1.17.1 + Vim

The resources

  • Google Web Fundamentals

Video download

Follow the wechat official account of “Front End Weekly” and reply to “course-async-await” to get the hd video tutorial download address.

digression

And finally, some of you might ask, why am I doing this open class? What’s next? Long time no see, I always feel like I owe you something