async/await-5. Correctly handle multiple await operations

async/await-5. Correctly handle multiple await operations

const fetch = require('node-fetch')

const sleep = (timeout = 2000) => new Promise(resolve => {
  setTimeout(resolve, timeout)
})

async function getZhihuColumn(id) {
  await sleep(2000)
  const url = `https://zhuanlan.zhihu.com/api/columns/${id}`
  const response = await fetch(url)
  return await response.json()
}

1. Two requests serially process multiple await operations, call the asynchronous operation first, and then process the asynchronous result

const showColumnInfo = async (id) => {
  console.time('showColumnInfo')
  const qianduanzhidian = await getZhihuColumn('qianduanzhidian')
  const FrontendMagazine = await getZhihuColumn('FrontendMagazine')
  console.log(`name:${qianduanzhidian.name}`)
  console.log(`description:${qianduanzhidian.description}`)
  console.log(`name:${FrontendMagazine.name}`)
  console.log(`description:${FrontendMagazine.description}`)
  console.timeEnd('showColumnInfo') // 4349.122ms
}
showColumnInfo()

2. Two requests are parallel

const showColumnInfo = async (id) => {
  console.time('showColumnInfo')
  const qianduanzhidianPromise = getZhihuColumn('qianduanzhidian')
  const FrontendMagazinePromise = getZhihuColumn('FrontendMagazine')

  const qianduanzhidian = await qianduanzhidianPromise
  const FrontendMagazine = await FrontendMagazinePromise
  console.log(`name:${qianduanzhidian.name}`)
  console.log(`description:${qianduanzhidian.description}`)
  console.log(`name:${FrontendMagazine.name}`)
  console.log(`description:${FrontendMagazine.description}`)
  console.timeEnd('showColumnInfo') // 2526.615ms
}
showColumnInfo()

Intelligent Recommendation

Async-await triggers (waits) multiple asynchronous operations simultaneously

Useasync-await Waiting for the asynchronous operation to complete, if there are no dependencies between the two asynchronous operations, triggering at the same time should be a better solution. Becaus...

async/await handles asynchronous operations

If the return value of a function is a promise, it can be optimized with async/await....

How to correctly use await for asynchronous operations in the loop

background problem Because the protocol often doesn’t know why it doesn’t give a batch delete interface, it can only delete a single cycle by itself. The following await in the map cycle i...

ASYNC and AWAIT read multiple files

The existing MD files are as follows: part1.md part2.md Fish encounter. Md Demand: Read the contents of the above three files and output. First, review how many files are implemented by reviewing how ...

Multiple Async / AWAIT error handling

Async / AWAIT is used in serial asynchronous requests, and there are two types of error handling. 1.try catch When an error occurred in the previous request, it will go directly to Catch, and the erro...

More Recommendation

Map traverses array correctly uses async and await

Question background When processing data, you often need to get a ixxxd to query in the XXX table, but the query is an asynchronous operation. NEWMATCHINFO has been empty in the data returned Analysis...

In Silverlight 5 project async / await

.Net 4.5 provides async / await the return synchronization allows asynchronous programming, however, async / await not only be used in .Net 4.5, by using the Async Targeting Pack will be in .Net 4.0 a...

The use of Async and await in MVC 5

HomeController.cs Index.cshtml GetList.cshtml GetListAsync.cshtml The results of the operation are as follows: Synchronous method execution time accumulates, asynchronous method execution time takes t...

Understand using async/await to handle asynchronous

Understand using async/await to handle asynchronous Async usage async Put it as a keyword in front of the function to indicate that the function is an asynchronous function becauseasync Asynchronous m...

Node - 24 - use async / await to handle asynchronous

principle Async functions Let's start with the async keyword, which is placed in front of a function. Just like this: In front of the functionasyncThe word means a simple thing: this function always r...

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

Top