Introduction to js asynchronous for loop

不言
Release: 2018-07-09 10:54:39
Original
1811 people have browsed it

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}]
Copy after login

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) } })
Copy after login

. 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

Usage of Javascript decorator

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!