当前位置:网站首页>Async principle implementation
Async principle implementation
2022-06-29 12:59:00 【Henry_ Phoebe】
Preface
If you don't know what to use, you can refer to async function , No more details here .
Children's shoes that want to see the principle , I need to know something about Generator function
principle
async Implementation is to Generator Functions and Automatic actuators , Wrapped in a function .
async function fn(args) {
// ...
}
function fn(args) {
return spawn(function* () {
// ...
})
}
be-all async Functions can be written in the second form above , among spawn A function is an automatic actuator .
function spawn(genF) {
return new Promise(function(resolve, reject) {
const gen = genF();
function step(nextF) {
let next;
try {
next = nextF();
} catch(e) {
return reject(e);
}
if(next.done) {
return resolve(next.value);
}
Promise.resolve(next.value).then(function(v) {
step(function() {
return gen.next(v); });
}, function(e) {
step(function() {
return gen.throw(e); });
});
}
step(function() {
return gen.next(undefined); });
});
}
Frequently asked questions
- Achieve one sleep
- Realize a traffic light : A red light 2 second , Yellow light 1 second , A green light 3 second
- Use async Realization Promise.all() The effect of
Achieve one sleep: every other 1 A second output 1, 2, 3, 4, 5
function sleep(interval) {
return new Promise(resolve => {
setTimeout(resolve, interval);
})
}
// usage
async function one2FiveInAsync() {
for (let i = 1; i <= 5; i++) {
console.log(i);
await sleep(1000);
}
}
one2FiveInAsync();
Realize a traffic light : A red light 2 second , Yellow light 1 second , A green light 3 second
function sleep(duration) {
return new Promise(resolve => {
setTimeout(resolve, duration);
})
}
async function changeColor(color, duration) {
console.log(' Current color ', color);
await sleep(duration);
}
async function main() {
await changeColor(' Red ', 2000);
await changeColor(' yellow ', 1000);
await changeColor(' green ', 3000);
}
main();
Use async Realization Promise.all() The effect of
async function test() {
const getFoo = (i) => {
return new Promise(resolve => setTimeout(() => {
resolve(i)
}, 1000))
}
// Writing a
// let values = await Promise.all([getFoo(1), getFoo(2), getFoo(3), getFoo(4)]);
// Write two
const f1 = getFoo(1)
const f2 = getFoo(2)
const f3 = getFoo(3)
const f4 = getFoo(4)
let values = [await f1, await f2, await f3, await f4]
console.log('values', values)
}
test()
边栏推荐
- Cocos star meetings at Hangzhou station in 2022
- STK_GLTF模型
- oracle 19c : change the user sys/system username pasword under Linux
- How to install oracle19c in Centos8
- Application Service Vulnerability scanning and exploitation of network security skills competition in secondary vocational schools (SSH private key disclosure)
- 2022.6.28-----leetcode.324
- Golang image/png 处理图片 旋转 写入
- 从Mpx资源构建优化看splitChunks代码分割
- 解决问题:ModuleNotFoundError: No module named ‘pip‘
- C#实现二叉排序树定义、插入、构造
猜你喜欢

中职网络安全技能竞赛之应用服务漏洞扫描与利用(SSH私钥泄露)

Earth observation satellite data

1. opencv realizes simple color recognition

QT signal and slot

Install the typescript environment and enable vscode to automatically monitor the compiled TS file as a JS file

Recurrence of recommended models (III): recall models youtubednn and DSSM

Application Service Vulnerability scanning and exploitation of network security skills competition in secondary vocational schools (SSH private key disclosure)

1. Opencv实现简单颜色识别

Blurred pictures become clear, one button two-color pictures, quickly organize local pictures These 8 online picture tools apply to join your favorites!

Interview shock 61: tell me about MySQL transaction isolation level?
随机推荐
Application Service Vulnerability scanning and exploitation of network security skills competition in secondary vocational schools (SSH private key disclosure)
Can software related inventions be patented in India?
Baidu cloud disk downloads large files without speed limit (valid for 2021-11 personal test)
nvtmpp
C#线索二叉树的定义
How to create new user for ORACLE 19c (CDB & PDB)
Viewing splitchunks code segmentation from MPX resource construction optimization
Cereal mall project
二十三、1-Bit数据的存储(延迟线/磁芯/DRAM/SRAM/磁带/磁盘/光盘/Flash SSD)
AGCO AI frontier promotion (6.29)
Bison uses error loop records
QT custom control: value range
MIT linear algebra Chinese Notes
如果我在深圳,到哪里开户比较好?另外想问,现在在线开户安全么?
Go question bank · 14 the pit of waitgroup
2022.6.28-----leetcode.324
Recurrence of recommended models (III): recall models youtubednn and DSSM
1. opencv realizes simple color recognition
Lm07 - detailed discussion on cross section strategy of futures
倍福PLC通过CANOpen通信控制伺服