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()
}
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()
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()
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...
If the return value of a function is a promise, it can be optimized with async/await....
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...
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 ...
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...
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...
.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...
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 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...
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...