async...await

ES8 introduces the async function, making asynchronous operations more convenient. Simply put, it is the syntactic sugar of the Generator function.

async function asyncFunc(params) {
  const result1 = await this.login()
  const result2 = await this.getInfo()
}
Is it more concise and easy to understand?

Variants

Asynchronous functions exist in the following four forms of use:

  • Function declaration:async function foo() {}
  • Function expression:const foo = async function() {}
  • The way the object:let obj = { async foo() {} }
  • Arrow function:const foo = async () => {}

Common usage summary

Handling a single asynchronous result:

async function asyncFunc() {
  const result = await otherAsyncFunc();
  console.log(result);
}

Process multiple asynchronous results in sequence:

async function asyncFunc() {
  const result1 = await otherAsyncFunc1();
  console.log(result1);
  const result2 = await otherAsyncFunc2();
  console.log(result2);
}

 

Process multiple asynchronous results in parallel:

 

async function asyncFunc() {
  const [result1, result2] = await Promise.all([
    otherAsyncFunc1(),
    otherAsyncFunc2()
  ]);
  console.log(result1, result2);
}

 

Handling errors:

async function asyncFunc() {
  try {
    await otherAsyncFunc();
  } catch (err) {
    console.error(err);
  }
}

 

 

Intelligent Recommendation

Database SQL combat 1: Find all the information of the latest employees

thought: The topic asks to find all the information of the latest employees. Through a sub-query (select max(hire_date) from employees), find the latest hire time of the employee, hire_date, and then ...

Correction of TOPSIS model based on entropy weight method

Recently, I am learning mathematical modeling and found a very good course on B station. It is very comprehensive and all the algorithms I often test involve:Qingfeng Mathematical Modeling This articl...

51nod longest common subsequence output path +

When x = 0 or y = 0 when f [x] [y] = 0 When a [x] = b [y] when f [x] [y] = f [x-1] [y-1] +1 When a [x]! = B [y] when f [x] [y] = max (f [x] [y-1], f [x-1] [y]) Note that the string 1 from the start, b...

Enumeration algorithm summary

I learned the enumeration algorithm in this section. The enumeration method is the nature of the problem itself, and all possible solutions are listed in one by one, and in the process of listed one b...

More Recommendation

Two value methods for Map of Java brushing knowledge points: keySet and entrySet, HashMap, Hashtable, TreeMap, LinkedHashMap, ConcurrentHashMap, Weak...

  The interface java.util.Map includes three implementation classes: HashMap, Hashtable, and TreeMap. Of course there are LinkedHashMap, ConcurrentHashMap, WeakHashMap.   Map is used to storeKey-value...

Everyday fresh items

Everyday fresh items - project preference Understand the e-commerce Web project development process Project demand analysis Page page Function map Deployment page Project framework Understand the e-co...

The easiest way: install opencv on windows platform python, that is to achieve import cv2 function

The following old method used before installed opencv, after reinstalling the system, reinstalled opencv according to the original method, and the result has always been reported: ImportError: Module ...

Matlab programming skills: reading and writing text files

Various text files are involved in MBD (model-based design), and the automated processing of text files can greatly improve work efficiency. This article briefly introduces the first step of processin...

Mysql opens a remote connection

1. Log in to Mysql cmd enter mysql -u root -pyoupassword (Input rule After you press Enter, you will be prompted to enter the password. Note that there may or may not be spaces before the user name, b...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top