当前位置:网站首页>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()
边栏推荐
- ZALSM_EXCEL_TO_INTERNAL_TABLE 导入数据大问题解决
- asp. Net project using aspnet_ compiler. Exe Publishing
- C#实现二叉树的先序遍历、中序遍历、后序遍历
- C#实现二叉树的层次遍历
- 缓存一致性,删除缓存,写入缓存,缓存击穿,缓存穿透,缓存雪崩
- C # implementation of binary tree non recursive middle order traversal program
- Interpolated scatter data
- C#实现顺序表定义、插入、删除、查找操作
- 【智能QbD风险评估工具】上海道宁为您带来LeanQbD介绍、试用、教程
- Bison uses error loop records
猜你喜欢

How can colleges and universities build future oriented smart campus based on cloud native? Full stack cloud native architecture vs traditional IT architecture

LeetCode_双指针_中等_328.奇偶链表

Recurrence of recommended models (IV): multi task models esmm and MMOE

MATLAB求极限

Adjacency matrix and adjacency table structure of C # realization graph

Interpolated scatter data

How to install oracle19c in Centos8

Aes-128-cbc-pkcs7padding encrypted PHP instance

Interview shock 61: tell me about MySQL transaction isolation level?

After class assignment of module 5 of the construction practice camp
随机推荐
如果我在深圳,到哪里开户比较好?另外想问,现在在线开户安全么?
2022.6.28-----leetcode.324
从Mpx资源构建优化看splitChunks代码分割
1. opencv realizes simple color recognition
C # implementation of binary tree non recursive middle order traversal program
After class assignment of module 5 of the construction practice camp
How to fix ora-01017: invalid user name / password login denied
LR、CR纽扣电池对照表
三维模型下载与动画控制
nvtmpp
超 Nice 的表格响应式布局小技巧
bind原理及模拟实现
Lm07 - detailed discussion on cross section strategy of futures
How to fix ORA-01017:用户名/口令无效 登录拒绝
qt 自定义控件 :取值范围
【云原生】2.4 Kubernetes 核心实战(中)
Comment calculer Win / Tai / Loss in paired t - test
Interview shock 61: tell me about MySQL transaction isolation level?
推荐模型复现(四):多任务模型ESMM、MMOE
Yunlong fire version aircraft battle (full version)