当前位置:网站首页>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)
})边栏推荐
- How to delete non system files on disk C. summary of files that can be deleted when disk C is popular
- 局域网SDN技术硬核内幕 - 15 三 从物到人 园区用户漫游的MPLS实现
- MySql的DDL和DML和DQL的基本语法
- 对线程池的了解与应用你掌握多少
- excel 将图片的链接URL 显示为图片 转
- Why is the computer screen yellowing? What is the reason for the yellowing of the monitor screen and the troubleshooting method
- Null and nullptr
- What if the file copied by SSD is only dozens of KB? Solution to slow copying speed after installing hard disk in computer
- LUR caching algorithm
- What if ICBC online banking assistant cannot be installed? ICBC online banking assistant installation failure solution
猜你喜欢

Nftscan and ATEM network have reached strategic cooperation in the field of NFT data

开源2周年,openGauss Developer Day 2022全程亮点回顾!

对线程池的了解与应用你掌握多少

【计网实验报告】Cisco局域网模拟组建、简单网络测试

初识Flutter中的Layer

【随笔】再见IE浏览器,一个时代的印记从此消失

Image processing solution veimagex technology evolution Road

常用机械设备安全虚拟仿真系统的应用场景及方案

编写一个具有搜索提示的搜索框

标签平滑(label smoothing)
随机推荐
对比学习下的跨模态语义对齐是最优的吗?---自适应稀疏化注意力对齐机制 IEEE Trans. MultiMedia
局域网SDN技术硬核内幕 - 14 三 从物到人——SDN走进园区网络
[C# 数组]-C# 之中的一维数组和对象数组
Cloudwego's design practice in the background platform of flybook management
接口-Fiddler-简介与安装
FileInputFormat.setInputPaths多路径读取规则
局域网SDN技术硬核内幕 - 3 前传 突破多核的瓶颈——虚拟化
Redis basic type common commands
基于API调用管理的SDN应用层DDoS攻击防御机制
LAN SDN technology hard core insider - what's in the prequel CPU?
【JDBC】报错Exception in thread “main”com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communica
图像处理解决方案 veImageX 技术演进之路
NFTScan 与 Atem Network 在 NFT 数据领域达成战略合作
LAN SDN technology hard core insider 13 II from LAN to Internet
64注意力机制 10章
How to solve the problem that telnet is not an internal or external command? Telnet is not an internal or external command solution
直播预告 | openGauss的自治运维平台DBMind实践分享
LAN SDN technology hard core insider - 02 forward multi-core technology for Moore's law for one second
redis基本类型常用命令
启牛老师说给开的vip账户安全吗?