
本文共 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 ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
发表评论
最新留言
关于作者
