This article mainly introduces the introduction of js asynchronous for loop, which has certain reference value. Now I share it with everyone. Friends in need can refer to it.
Assume that a chef needs to make 3 dishes Dishes, declare an array of dishes. The dish object is the name of the dish and the time required to cook it.
let dishes=[{name:"fish",time:1},{name:"fish1",time:2},{name:"fish3",time:3}]
First of all, the chef should cook the dishes one after another, so he must ensure that one dish is finished before starting the next dish. This is implemented using async/await. Of course, you can also choose to use Promise
(async ()=>{ for (let d of ds) { console.log("开始做"+d.name) await (() => { return new Promise(res => { setTimeout(res, d.time * 1000) }) })(); console.log("做好了"+d.name) } })
. The above is the entire content of this article. I hope it will be helpful to everyone's learning. For more related content, please pay attention to the PHP Chinese website!
Related recommendations:
jQuery-Ajax requests Json data and loads it on the front-end page
##ES6 Class inheritance and super Introduction
The above is the detailed content of Introduction to js asynchronous for loop. For more information, please follow other related articles on the PHP Chinese website!