异步Promise及Async/Await 异步之神详解
发布日期:2021-05-10 03:28:43 浏览次数:20 分类:精选文章

本文共 4220 字,大约阅读时间需要 14 分钟。

���������

Async/Await ��� Promise ���������������������������������������

������������������ Async/Await���

1.1 ������ Promise ��� Async/Await

Async/Await ������������

���������������������������Async���������await������������������������ Promise���Async/await ��� Promise ������������������ Promise ���������������Async ��� await ��������������������������������������������������������������� Promise ������������

1.2 Async ���������

��������������������������������������� async ������������������������������������ async ��������������������������������������������������� Promise������������������ async ��������� Promise ������������������������

������������������������

const async demo = function () { /*...*/ };
// ������
async function () {/*...*/ };

1.3 await ������������������

await ������������������������������������������������������������������������������������������������������ await ������������������

������������������������

const demo = async () => {
const result = await setDelay(1000);
console.log(result);
console.log(await setDelay(1000));
console.log('������');
};
demo();

1.4 async/await ���������

2.1 ������ Promise ��� Async/Await

Async/await ������������

���������������������������Async���������await������������������������ Promise���Async ��� await ��������������������������������������������������������������� Promise ������������

2.2 Async ���������������

��������������������������������������� async ������������������������������������ async ��������������������������������������������������� Promise������������������ async ��������� Promise ������������������������

������������������������

const async demo = function () { /*...*/ };
// ������
async function () {/*...*/ };

2.3 await ������������������

await ������������������������������������������������������������������������������������������������������ await ������������������

������������������������

const demo = async () => {
const result = await setDelay(1000);
console.log(result);
console.log(await setDelay(1000));
console.log('������');
};
demo();

2.4 async/await ���������

������Async/await ������������

3.1 ������������������

������ async ������������������������ Promise������������������������������ catch ���������������

���������������

const demo = async () => {
const result = await setDelay(1000);
console.log(result);
console.log(await setDelay(1000));
console.log('������');
};
demo().catch(err => {
console.log(err);
});

3.2 try...catch ������������

Uncaught SyntaxError: await ��������� async ������������������������������������������������������ try ��������������������������������������������������������������� try...catch ���������������������������

������Async/Await ���������������������������

4.1 ��������������������������� then ������������

������������������ Promise.all��������������������� Request������������������������������������ then������������������������������������

��������������������������������������������������������������������������������� Promise ���������������������

4.2 ��������������������������� for ������

������������������������������������������������������������������������

const temp = arr[0](1000);
for (let i = 1; i < arr.length; i++) {
temp = temp.then((result) => {
console.log(result);
return arr[i](1000);
});
}

4.3 ��������������������������� async/await ������

��� async/await ���������������������������������������������

const arr = [timeout(2000), timeout(1000), timeout(1000)];
(async () => {
for (let i = 0; i < arr.length; i++) {
const result = await arr[i]();
console.log(result);
}
})();

5 ������������������

5.1 ������������������������������������������ Promise ���������������������������������������������������

5.2 ������ finally ��� catch finale ������������������������������������������������������������������������������

5.3 ������������������������������������������������������������������������������������������������������

��������������������������������� Async/Await ��� Promise ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������

上一篇:3、组件间的通信
下一篇:C++ 底层语言的信仰-指针分类

发表评论

最新留言

哈哈,博客排版真的漂亮呢~
[***.90.31.176]2025年04月28日 03时34分29秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章