当前位置:网站首页>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()
边栏推荐
- MFC dialog program core -isdialogmessage function -msg message structure -getmessage function -dispatchmessage function
- async原理实现
- How to create new user for ORACLE 19c (CDB & PDB)
- InDesign plug-in - general function development -js debugger open and close -js script development -id plug-in
- AGCO AI frontier promotion (6.29)
- Interview shock 61: tell me about MySQL transaction isolation level?
- C#通过线索二叉树进行中序遍历输出
- AES-128-CBC-Pkcs7Padding加密PHP实例
- [JUC series] ThreadLocal of synchronization tool class
- How to fix ora-01017: invalid user name / password login denied
猜你喜欢

推荐模型复现(四):多任务模型ESMM、MMOE

缓存一致性,删除缓存,写入缓存,缓存击穿,缓存穿透,缓存雪崩

MATLAB求极限

Interpolated scatter data

go 学习-搭建开发环境vscode开发环境golang

Kyligence Zen, an intelligent indicator driven management and decision-making platform, is newly launched and is in limited internal testing

Simple introduction to matlab

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

OPC of Beifu twincat3_ UA communication test case

在印度与软件相关的发明可不可以申请专利?
随机推荐
InDesign plug-in - general function development -js debugger open and close -js script development -id plug-in
MFC-对话框程序核心-IsDialogMessage函数-MSG 消息结构-GetMessage函数-DispatchMessage函数
Interpolated scatter data
C#通过线索二叉树进行中序遍历输出
C # indexe l'arbre binaire en traversant l'ordre moyen
OPC of Beifu twincat3_ UA communication test case
STK_GLTF模型
从Mpx资源构建优化看splitChunks代码分割
Can software related inventions be patented in India?
倍福控制第三方伺服走CSV模式--以汇川伺服为例
How to create new user for ORACLE 19c (CDB & PDB)
Qt的信号与槽
中职网络安全技能竞赛之应用服务漏洞扫描与利用(SSH私钥泄露)
QQ group was stolen, a large-scale social death scene caught off guard
ArcGIS中对面状河流进行等距分段【渐变赋色、污染物扩散】
Proteus Software beginner notes
go 学习-搭建开发环境vscode开发环境golang
Difficult conversation breaks through the bottleneck of conversation and achieves perfect communication
nvtmpp
UI file introduction in QT