当前位置:网站首页>ES6 solves asynchronous problems
ES6 solves asynchronous problems
2022-07-23 07:26:00 【Guo Guo Han Han】
ES6 The asynchronous problem is solved by promise,promise There are three parameters in , Namely resolve、reject and finally, When the function is successfully executed, execute resolve, Failed execution reject, It will be implemented in the end finally
Let me introduce you resolve. To execute resolve perform . Then use (.then), It stands for resolve Function of ,
(.catch) It stands for reject, Use setTimeOut The reason for the function is to simulate the data transmitted by the backend ,flag It is also to simulate the success or failure of courtship , If you want to use it all the time (.XXX) Then we must ensure that the function return Is a promise, Only promise Functions have then、catch and finally
function fn(){
return new Promise((resolve) => {
setTimeout(function(){
if(flag){
resolve();
}else{
reject();
}
},1000)
})
}
fn().then(){
console.log("123")
return fn()
}
.catch(){
console.log("456")
return fn()
}
.finally(){
console.log("789")
}Let's start with promise Example , This example is to output every picture every second
let div = document.querySelector("div")
function delayImg(item,src){
return new Promise((resolve) => {
setTimeout(function(){
let img = document.createElement("img");
img.src = src;
item.append(img);
resolve();
},1000)
})
}
delayImg(div,'https://s1.ax1x.com/2022/07/20/jbJGtg.jpg')
.then(function(){
return delayImg(div,'https://s1.ax1x.com/2022/07/20/jbJtpj.jpg')
})
.then(function(){
return delayImg(div,'https://s1.ax1x.com/2022/07/20/jbJJhQ.jpg')
})Then do a little exercise to think about the priority of some outputs
function cook() {
console.log(' Start cooking .');
var p = new Promise(function(resolve, reject){
setTimeout(function() {
console.log(' After cooking !');
resolve(' Stir-Fried Rice with Egg ');
}, 1000);
});
return p;
}
function eat(data) {
console.log(' Start eating :' + data);
var p = new Promise(function(resolve, reject) {
setTimeout(function() {
console.log(' After dinner !');
resolve(' A bowl and a pair of chopsticks ');
}, 2000);
});
return p;
}
function wash(data) {
console.log(' Start washing dishes :' + data);
var p = new Promise(function(resolve, reject) {
setTimeout(function() {
console.log(' Finished washing dishes !');
resolve(' Clean bowls and chopsticks ');
}, 2000);
});
Think about how to make him output in order on the console , Pictured

This exercise can make you think promise Medium priority issues , Deepen your understanding of promise The understanding of the
answer :
cook()
.then(function(sentence){
return eat(sentence)
})
.then(function(sentence1){
return wash(sentence1)
}).then(function(sentence2){
console.log(sentence2)
})边栏推荐
- 一根网线怎么连接两台电脑传文件 一根网线连接两台电脑的方法
- 对比学习下的跨模态语义对齐是最优的吗?---自适应稀疏化注意力对齐机制 IEEE Trans. MultiMedia
- CAN总线详解
- 导出功能单独调用
- Livegbs design document of security camera internet live broadcast scheme
- ES6新增的class类
- 支持多数不规则用户的隐私保护联邦学习框架
- Why is the computer screen yellowing? What is the reason for the yellowing of the monitor screen and the troubleshooting method
- VIM text editor
- 10个Live Demo都展示了啥?看过没看过的都值得再看一遍
猜你喜欢

After 100 billion of revenue, Alibaba cloud ecosystem has a new way to play

浅析缓存的读写策略

Shell script

图像处理解决方案 veImageX 技术演进之路

A survey of copyright protection research on deep learning model

Q6ui layout operation

正向代理,反向代理及XFF

Esphone's self-made infrared remote control is connected to ha to control lights, switches, etc. any remote control can be used

Livegbs design document of security camera internet live broadcast scheme

千亿营收之后,阿里云生态有了新打法
随机推荐
LAN SDN technology hard core insider 13 II from LAN to Internet
Overview of the development of pseudo defense in cyberspace: from pseudo concept to "pseudo +" ecology
AE常用表达式汇总「建议收藏」
dispatch_once 的秘密
面向商用活体检测平台的鲁棒性评估
常用机械设备安全虚拟仿真系统的应用场景及方案
C language file operation (including all knowledge points, detailed explanation of related functions and codes)
String in SQL Server_ Implementation of split function
初识Flutter中的Layer
启牛老师说给开的vip账户安全吗?
Zhimeng dedecms forgot to manage the background password retrieval method
说说Flutter中的RepaintBoundary
Microsoft, how can't we upload photos? What's the matter? The solution of twinsornot photos not being transmitted
C language program environment
redis常用基础配置文件
Esphone's self-made infrared remote control is connected to ha to control lights, switches, etc. any remote control can be used
C语言 程序环境
ESP32教程(一):VScode+platformIO和Vscode+ESP-IDF两种开发环境搭建
接口-Fiddler-简介与安装
W25Q128FV译文(二)