将介绍js关于异步的事,给你整的明明白白的,安排
TODO c+数字:需要修复的地方
个人理解:
异步,需要等待个结果,才能干其他事的事,都是异步。
举个例子:1.考完需要等待考试结果才能填报志愿。

promise

Promise啥用

本质上 Promise 是一个函数返回的对象,我们可以在它上面绑定回调函数,这样我们就不需要在一开始把回调函数作为参数传入这个函数了。
官方例子:
假设现在有一个名为 createAudioFileAsync() 的函数,它接收一些配置和两个回调函数,然后异步地生成音频文件。一个回调函数在文件成功创建时被调用,另一个则在出现异常时被调用。

以下为使用 createAudioFileAsync() 的示例:

// 成功的回调函数
function successCallback(result) {console.log("音频文件创建成功: " + result);
}// 失败的回调函数
function failureCallback(error) {console.log("音频文件创建失败: " + error);
}createAudioFileAsync(audioSettings, successCallback, failureCallback);

更现代的函数会返回一个 Promise 对象,使得你可以将你的回调函数绑定在该 Promise 上。

如果函数 createAudioFileAsync() 被重写为返回 Promise 的形式,那么我们可以像下面这样简单地调用它:

const promise = createAudioFileAsync(audioSettings);
promise.then(successCallback, failureCallback);
或者简写为:createAudioFileAsync(audioSettings).then(successCallback, failureCallback);

我们把这个称为 异步函数调用,这种形式有若干优点,下面我们将会逐一讨论。

优点

1.在本轮 事件循环 运行完成之前,回调函数是不会被调用的。
2.即使异步操作已经完成(成功或失败),在这之后通过 then() 添加的回调函数也会被调用。
3.通过多次调用 then() 可以添加多个回调函数,它们会按照插入顺序进行执行。

怎么用Promise

连续执行两个或者多个异步操作是一个常见的需求,在上一个操作执行成功之后,开始下一个的操作,并带着上一步操作所返回的结果。我们可以通过创造一个 Promise 链来实现这种需求。
开始表演

链式调用

开始表演
亚古兽恐怖进化(狂暴骷髅形态):

doSomething(function(result) {doSomethingElse(result, function(newResult) {doThirdThing(newResult, function(finalResult) {console.log('Got the final result: ' + finalResult);}, failureCallback);}, failureCallback);
}, failureCallback);

亚古兽进化(机械暴龙兽):

doSomething().then(function(result) {return doSomethingElse(result);
})
.then(function(newResult) {return doThirdThing(newResult);
})
.then(function(finalResult) {console.log('Got the final result: ' + finalResult);
})
.catch(failureCallback);

亚古兽究极进化(机械暴龙兽):

then 里的参数是可选的,catch(failureCallback) 是 then(null, failureCallback) 的缩略形式。还用啦箭头函数

doSomething()
.then(result => doSomethingElse(result))
.then(newResult => doThirdThing(newResult))
.then(finalResult => {console.log(`Got the final result: ${finalResult}`);
})
.catch(failureCallback);

注意:一定要有返回值,否则,callback 将无法获取上一个 Promise 的结果。(如果使用箭头函数,() => x 比 () => { return x; } 更简洁一些,但后一种保留 return 的写法才支持使用多个语句。)。

Catch 的后续链式操作

有可能会在一个回调失败之后继续使用链式操作,即,使用一个 catch,这对于在链式操作中抛出一个失败之后,再次进行新的操作会很有用。请阅读下面的例子:

new Promise((resolve, reject) => {console.log('初始化');resolve();
})
.then(() => {throw new Error('有哪里不对了');console.log('执行「这个」”');
})
.catch(() => {console.log('执行「那个」');
})
.then(() => {console.log('执行「这个」,无论前面发生了什么');
});

输出结果如下:

初始化
执行“那个”
执行“这个”,无论前面发生了什么

上面例子总结:
1.throw—>抛出错误,具有return意思
2.catch—>在前面任何一个链式失败的情况下,绕过错误,不会抛出错误,直接执行catch内容和后面的then;
如果此时去掉catch(),直接是then就会抛出错误,并停止执行下面的东西;
报错的then和catch之间的then都不会执行。

错误传递

前面“亚古兽错误进化”----函数回调地狱与使用Promise“亚古兽究极进化”相比:
1.promise只在尾部调用一次failureCallback (错误后的回调)
与他有异曲同工之妙的有下面两个的例子:

try {let result = syncDoSomething();let newResult = syncDoSomethingElse(result);let finalResult = syncDoThirdThing(newResult);console.log(`Got the final result: ${finalResult}`);
} catch(error) {failureCallback(error);
}
async function foo() {try {const result = await doSomething();const newResult = await doSomethingElse(result);const finalResult = await doThirdThing(newResult);console.log(`Got the final result: ${finalResult}`);} catch(error) {failureCallback(error);}
}

注意:下面会详细讲async/await语法

Promise 拒绝事件

当 Promise 被拒绝时,会有下文所述的两个事件之一被派发到全局作用域(通常而言,就是window;如果是在 web worker 中使用的话,就是 Worker 或者其他 worker-based 接口)。这两个事件如下所示:

#mermaid-svg-BJab1h4zAiJpDqec .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-BJab1h4zAiJpDqec .label text{fill:#333}#mermaid-svg-BJab1h4zAiJpDqec .node rect,#mermaid-svg-BJab1h4zAiJpDqec .node circle,#mermaid-svg-BJab1h4zAiJpDqec .node ellipse,#mermaid-svg-BJab1h4zAiJpDqec .node polygon,#mermaid-svg-BJab1h4zAiJpDqec .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-BJab1h4zAiJpDqec .node .label{text-align:center;fill:#333}#mermaid-svg-BJab1h4zAiJpDqec .node.clickable{cursor:pointer}#mermaid-svg-BJab1h4zAiJpDqec .arrowheadPath{fill:#333}#mermaid-svg-BJab1h4zAiJpDqec .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-BJab1h4zAiJpDqec .flowchart-link{stroke:#333;fill:none}#mermaid-svg-BJab1h4zAiJpDqec .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-BJab1h4zAiJpDqec .edgeLabel rect{opacity:0.9}#mermaid-svg-BJab1h4zAiJpDqec .edgeLabel span{color:#333}#mermaid-svg-BJab1h4zAiJpDqec .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-BJab1h4zAiJpDqec .cluster text{fill:#333}#mermaid-svg-BJab1h4zAiJpDqec div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-BJab1h4zAiJpDqec .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-BJab1h4zAiJpDqec text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-BJab1h4zAiJpDqec .actor-line{stroke:grey}#mermaid-svg-BJab1h4zAiJpDqec .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-BJab1h4zAiJpDqec .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-BJab1h4zAiJpDqec #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-BJab1h4zAiJpDqec .sequenceNumber{fill:#fff}#mermaid-svg-BJab1h4zAiJpDqec #sequencenumber{fill:#333}#mermaid-svg-BJab1h4zAiJpDqec #crosshead path{fill:#333;stroke:#333}#mermaid-svg-BJab1h4zAiJpDqec .messageText{fill:#333;stroke:#333}#mermaid-svg-BJab1h4zAiJpDqec .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-BJab1h4zAiJpDqec .labelText,#mermaid-svg-BJab1h4zAiJpDqec .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-BJab1h4zAiJpDqec .loopText,#mermaid-svg-BJab1h4zAiJpDqec .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-BJab1h4zAiJpDqec .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-BJab1h4zAiJpDqec .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-BJab1h4zAiJpDqec .noteText,#mermaid-svg-BJab1h4zAiJpDqec .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-BJab1h4zAiJpDqec .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-BJab1h4zAiJpDqec .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-BJab1h4zAiJpDqec .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-BJab1h4zAiJpDqec .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-BJab1h4zAiJpDqec .section{stroke:none;opacity:0.2}#mermaid-svg-BJab1h4zAiJpDqec .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-BJab1h4zAiJpDqec .section2{fill:#fff400}#mermaid-svg-BJab1h4zAiJpDqec .section1,#mermaid-svg-BJab1h4zAiJpDqec .section3{fill:#fff;opacity:0.2}#mermaid-svg-BJab1h4zAiJpDqec .sectionTitle0{fill:#333}#mermaid-svg-BJab1h4zAiJpDqec .sectionTitle1{fill:#333}#mermaid-svg-BJab1h4zAiJpDqec .sectionTitle2{fill:#333}#mermaid-svg-BJab1h4zAiJpDqec .sectionTitle3{fill:#333}#mermaid-svg-BJab1h4zAiJpDqec .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-BJab1h4zAiJpDqec .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-BJab1h4zAiJpDqec .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-BJab1h4zAiJpDqec .grid path{stroke-width:0}#mermaid-svg-BJab1h4zAiJpDqec .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-BJab1h4zAiJpDqec .task{stroke-width:2}#mermaid-svg-BJab1h4zAiJpDqec .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-BJab1h4zAiJpDqec .taskText:not([font-size]){font-size:11px}#mermaid-svg-BJab1h4zAiJpDqec .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-BJab1h4zAiJpDqec .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-BJab1h4zAiJpDqec .task.clickable{cursor:pointer}#mermaid-svg-BJab1h4zAiJpDqec .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-BJab1h4zAiJpDqec .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-BJab1h4zAiJpDqec .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-BJab1h4zAiJpDqec .taskText0,#mermaid-svg-BJab1h4zAiJpDqec .taskText1,#mermaid-svg-BJab1h4zAiJpDqec .taskText2,#mermaid-svg-BJab1h4zAiJpDqec .taskText3{fill:#fff}#mermaid-svg-BJab1h4zAiJpDqec .task0,#mermaid-svg-BJab1h4zAiJpDqec .task1,#mermaid-svg-BJab1h4zAiJpDqec .task2,#mermaid-svg-BJab1h4zAiJpDqec .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-BJab1h4zAiJpDqec .taskTextOutside0,#mermaid-svg-BJab1h4zAiJpDqec .taskTextOutside2{fill:#000}#mermaid-svg-BJab1h4zAiJpDqec .taskTextOutside1,#mermaid-svg-BJab1h4zAiJpDqec .taskTextOutside3{fill:#000}#mermaid-svg-BJab1h4zAiJpDqec .active0,#mermaid-svg-BJab1h4zAiJpDqec .active1,#mermaid-svg-BJab1h4zAiJpDqec .active2,#mermaid-svg-BJab1h4zAiJpDqec .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-BJab1h4zAiJpDqec .activeText0,#mermaid-svg-BJab1h4zAiJpDqec .activeText1,#mermaid-svg-BJab1h4zAiJpDqec .activeText2,#mermaid-svg-BJab1h4zAiJpDqec .activeText3{fill:#000 !important}#mermaid-svg-BJab1h4zAiJpDqec .done0,#mermaid-svg-BJab1h4zAiJpDqec .done1,#mermaid-svg-BJab1h4zAiJpDqec .done2,#mermaid-svg-BJab1h4zAiJpDqec .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-BJab1h4zAiJpDqec .doneText0,#mermaid-svg-BJab1h4zAiJpDqec .doneText1,#mermaid-svg-BJab1h4zAiJpDqec .doneText2,#mermaid-svg-BJab1h4zAiJpDqec .doneText3{fill:#000 !important}#mermaid-svg-BJab1h4zAiJpDqec .crit0,#mermaid-svg-BJab1h4zAiJpDqec .crit1,#mermaid-svg-BJab1h4zAiJpDqec .crit2,#mermaid-svg-BJab1h4zAiJpDqec .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-BJab1h4zAiJpDqec .activeCrit0,#mermaid-svg-BJab1h4zAiJpDqec .activeCrit1,#mermaid-svg-BJab1h4zAiJpDqec .activeCrit2,#mermaid-svg-BJab1h4zAiJpDqec .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-BJab1h4zAiJpDqec .doneCrit0,#mermaid-svg-BJab1h4zAiJpDqec .doneCrit1,#mermaid-svg-BJab1h4zAiJpDqec .doneCrit2,#mermaid-svg-BJab1h4zAiJpDqec .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-BJab1h4zAiJpDqec .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-BJab1h4zAiJpDqec .milestoneText{font-style:italic}#mermaid-svg-BJab1h4zAiJpDqec .doneCritText0,#mermaid-svg-BJab1h4zAiJpDqec .doneCritText1,#mermaid-svg-BJab1h4zAiJpDqec .doneCritText2,#mermaid-svg-BJab1h4zAiJpDqec .doneCritText3{fill:#000 !important}#mermaid-svg-BJab1h4zAiJpDqec .activeCritText0,#mermaid-svg-BJab1h4zAiJpDqec .activeCritText1,#mermaid-svg-BJab1h4zAiJpDqec .activeCritText2,#mermaid-svg-BJab1h4zAiJpDqec .activeCritText3{fill:#000 !important}#mermaid-svg-BJab1h4zAiJpDqec .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-BJab1h4zAiJpDqec g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-BJab1h4zAiJpDqec g.classGroup text .title{font-weight:bolder}#mermaid-svg-BJab1h4zAiJpDqec g.clickable{cursor:pointer}#mermaid-svg-BJab1h4zAiJpDqec g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-BJab1h4zAiJpDqec g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-BJab1h4zAiJpDqec .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-BJab1h4zAiJpDqec .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-BJab1h4zAiJpDqec .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-BJab1h4zAiJpDqec .dashed-line{stroke-dasharray:3}#mermaid-svg-BJab1h4zAiJpDqec #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-BJab1h4zAiJpDqec #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-BJab1h4zAiJpDqec #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-BJab1h4zAiJpDqec #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-BJab1h4zAiJpDqec #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-BJab1h4zAiJpDqec #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-BJab1h4zAiJpDqec #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-BJab1h4zAiJpDqec #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-BJab1h4zAiJpDqec .commit-id,#mermaid-svg-BJab1h4zAiJpDqec .commit-msg,#mermaid-svg-BJab1h4zAiJpDqec .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-BJab1h4zAiJpDqec .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-BJab1h4zAiJpDqec .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-BJab1h4zAiJpDqec g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-BJab1h4zAiJpDqec g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-BJab1h4zAiJpDqec g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-BJab1h4zAiJpDqec g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-BJab1h4zAiJpDqec g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-BJab1h4zAiJpDqec g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-BJab1h4zAiJpDqec .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-BJab1h4zAiJpDqec .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-BJab1h4zAiJpDqec .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-BJab1h4zAiJpDqec .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-BJab1h4zAiJpDqec .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-BJab1h4zAiJpDqec .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-BJab1h4zAiJpDqec .edgeLabel text{fill:#333}#mermaid-svg-BJab1h4zAiJpDqec .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-BJab1h4zAiJpDqec .node circle.state-start{fill:black;stroke:black}#mermaid-svg-BJab1h4zAiJpDqec .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-BJab1h4zAiJpDqec #statediagram-barbEnd{fill:#9370db}#mermaid-svg-BJab1h4zAiJpDqec .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-BJab1h4zAiJpDqec .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-BJab1h4zAiJpDqec .statediagram-state .divider{stroke:#9370db}#mermaid-svg-BJab1h4zAiJpDqec .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-BJab1h4zAiJpDqec .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-BJab1h4zAiJpDqec .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-BJab1h4zAiJpDqec .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-BJab1h4zAiJpDqec .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-BJab1h4zAiJpDqec .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-BJab1h4zAiJpDqec .note-edge{stroke-dasharray:5}#mermaid-svg-BJab1h4zAiJpDqec .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-BJab1h4zAiJpDqec .error-icon{fill:#522}#mermaid-svg-BJab1h4zAiJpDqec .error-text{fill:#522;stroke:#522}#mermaid-svg-BJab1h4zAiJpDqec .edge-thickness-normal{stroke-width:2px}#mermaid-svg-BJab1h4zAiJpDqec .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-BJab1h4zAiJpDqec .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-BJab1h4zAiJpDqec .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-BJab1h4zAiJpDqec .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-BJab1h4zAiJpDqec .marker{fill:#333}#mermaid-svg-BJab1h4zAiJpDqec .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;} #mermaid-svg-BJab1h4zAiJpDqec {color: rgba(0, 0, 0, 0.75);font: ;}

拒绝
reject
unhandledrejection
没有提供 reject 函数来处理该 rejection
rejectionhandled
#mermaid-svg-vUZkeLPeN0IzzhlY .label{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);fill:#333;color:#333}#mermaid-svg-vUZkeLPeN0IzzhlY .label text{fill:#333}#mermaid-svg-vUZkeLPeN0IzzhlY .node rect,#mermaid-svg-vUZkeLPeN0IzzhlY .node circle,#mermaid-svg-vUZkeLPeN0IzzhlY .node ellipse,#mermaid-svg-vUZkeLPeN0IzzhlY .node polygon,#mermaid-svg-vUZkeLPeN0IzzhlY .node path{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-vUZkeLPeN0IzzhlY .node .label{text-align:center;fill:#333}#mermaid-svg-vUZkeLPeN0IzzhlY .node.clickable{cursor:pointer}#mermaid-svg-vUZkeLPeN0IzzhlY .arrowheadPath{fill:#333}#mermaid-svg-vUZkeLPeN0IzzhlY .edgePath .path{stroke:#333;stroke-width:1.5px}#mermaid-svg-vUZkeLPeN0IzzhlY .flowchart-link{stroke:#333;fill:none}#mermaid-svg-vUZkeLPeN0IzzhlY .edgeLabel{background-color:#e8e8e8;text-align:center}#mermaid-svg-vUZkeLPeN0IzzhlY .edgeLabel rect{opacity:0.9}#mermaid-svg-vUZkeLPeN0IzzhlY .edgeLabel span{color:#333}#mermaid-svg-vUZkeLPeN0IzzhlY .cluster rect{fill:#ffffde;stroke:#aa3;stroke-width:1px}#mermaid-svg-vUZkeLPeN0IzzhlY .cluster text{fill:#333}#mermaid-svg-vUZkeLPeN0IzzhlY div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:12px;background:#ffffde;border:1px solid #aa3;border-radius:2px;pointer-events:none;z-index:100}#mermaid-svg-vUZkeLPeN0IzzhlY .actor{stroke:#ccf;fill:#ECECFF}#mermaid-svg-vUZkeLPeN0IzzhlY text.actor>tspan{fill:#000;stroke:none}#mermaid-svg-vUZkeLPeN0IzzhlY .actor-line{stroke:grey}#mermaid-svg-vUZkeLPeN0IzzhlY .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333}#mermaid-svg-vUZkeLPeN0IzzhlY .messageLine1{stroke-width:1.5;stroke-dasharray:2, 2;stroke:#333}#mermaid-svg-vUZkeLPeN0IzzhlY #arrowhead path{fill:#333;stroke:#333}#mermaid-svg-vUZkeLPeN0IzzhlY .sequenceNumber{fill:#fff}#mermaid-svg-vUZkeLPeN0IzzhlY #sequencenumber{fill:#333}#mermaid-svg-vUZkeLPeN0IzzhlY #crosshead path{fill:#333;stroke:#333}#mermaid-svg-vUZkeLPeN0IzzhlY .messageText{fill:#333;stroke:#333}#mermaid-svg-vUZkeLPeN0IzzhlY .labelBox{stroke:#ccf;fill:#ECECFF}#mermaid-svg-vUZkeLPeN0IzzhlY .labelText,#mermaid-svg-vUZkeLPeN0IzzhlY .labelText>tspan{fill:#000;stroke:none}#mermaid-svg-vUZkeLPeN0IzzhlY .loopText,#mermaid-svg-vUZkeLPeN0IzzhlY .loopText>tspan{fill:#000;stroke:none}#mermaid-svg-vUZkeLPeN0IzzhlY .loopLine{stroke-width:2px;stroke-dasharray:2, 2;stroke:#ccf;fill:#ccf}#mermaid-svg-vUZkeLPeN0IzzhlY .note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-vUZkeLPeN0IzzhlY .noteText,#mermaid-svg-vUZkeLPeN0IzzhlY .noteText>tspan{fill:#000;stroke:none}#mermaid-svg-vUZkeLPeN0IzzhlY .activation0{fill:#f4f4f4;stroke:#666}#mermaid-svg-vUZkeLPeN0IzzhlY .activation1{fill:#f4f4f4;stroke:#666}#mermaid-svg-vUZkeLPeN0IzzhlY .activation2{fill:#f4f4f4;stroke:#666}#mermaid-svg-vUZkeLPeN0IzzhlY .mermaid-main-font{font-family:"trebuchet ms", verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vUZkeLPeN0IzzhlY .section{stroke:none;opacity:0.2}#mermaid-svg-vUZkeLPeN0IzzhlY .section0{fill:rgba(102,102,255,0.49)}#mermaid-svg-vUZkeLPeN0IzzhlY .section2{fill:#fff400}#mermaid-svg-vUZkeLPeN0IzzhlY .section1,#mermaid-svg-vUZkeLPeN0IzzhlY .section3{fill:#fff;opacity:0.2}#mermaid-svg-vUZkeLPeN0IzzhlY .sectionTitle0{fill:#333}#mermaid-svg-vUZkeLPeN0IzzhlY .sectionTitle1{fill:#333}#mermaid-svg-vUZkeLPeN0IzzhlY .sectionTitle2{fill:#333}#mermaid-svg-vUZkeLPeN0IzzhlY .sectionTitle3{fill:#333}#mermaid-svg-vUZkeLPeN0IzzhlY .sectionTitle{text-anchor:start;font-size:11px;text-height:14px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vUZkeLPeN0IzzhlY .grid .tick{stroke:#d3d3d3;opacity:0.8;shape-rendering:crispEdges}#mermaid-svg-vUZkeLPeN0IzzhlY .grid .tick text{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vUZkeLPeN0IzzhlY .grid path{stroke-width:0}#mermaid-svg-vUZkeLPeN0IzzhlY .today{fill:none;stroke:red;stroke-width:2px}#mermaid-svg-vUZkeLPeN0IzzhlY .task{stroke-width:2}#mermaid-svg-vUZkeLPeN0IzzhlY .taskText{text-anchor:middle;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vUZkeLPeN0IzzhlY .taskText:not([font-size]){font-size:11px}#mermaid-svg-vUZkeLPeN0IzzhlY .taskTextOutsideRight{fill:#000;text-anchor:start;font-size:11px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vUZkeLPeN0IzzhlY .taskTextOutsideLeft{fill:#000;text-anchor:end;font-size:11px}#mermaid-svg-vUZkeLPeN0IzzhlY .task.clickable{cursor:pointer}#mermaid-svg-vUZkeLPeN0IzzhlY .taskText.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-vUZkeLPeN0IzzhlY .taskTextOutsideLeft.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-vUZkeLPeN0IzzhlY .taskTextOutsideRight.clickable{cursor:pointer;fill:#003163 !important;font-weight:bold}#mermaid-svg-vUZkeLPeN0IzzhlY .taskText0,#mermaid-svg-vUZkeLPeN0IzzhlY .taskText1,#mermaid-svg-vUZkeLPeN0IzzhlY .taskText2,#mermaid-svg-vUZkeLPeN0IzzhlY .taskText3{fill:#fff}#mermaid-svg-vUZkeLPeN0IzzhlY .task0,#mermaid-svg-vUZkeLPeN0IzzhlY .task1,#mermaid-svg-vUZkeLPeN0IzzhlY .task2,#mermaid-svg-vUZkeLPeN0IzzhlY .task3{fill:#8a90dd;stroke:#534fbc}#mermaid-svg-vUZkeLPeN0IzzhlY .taskTextOutside0,#mermaid-svg-vUZkeLPeN0IzzhlY .taskTextOutside2{fill:#000}#mermaid-svg-vUZkeLPeN0IzzhlY .taskTextOutside1,#mermaid-svg-vUZkeLPeN0IzzhlY .taskTextOutside3{fill:#000}#mermaid-svg-vUZkeLPeN0IzzhlY .active0,#mermaid-svg-vUZkeLPeN0IzzhlY .active1,#mermaid-svg-vUZkeLPeN0IzzhlY .active2,#mermaid-svg-vUZkeLPeN0IzzhlY .active3{fill:#bfc7ff;stroke:#534fbc}#mermaid-svg-vUZkeLPeN0IzzhlY .activeText0,#mermaid-svg-vUZkeLPeN0IzzhlY .activeText1,#mermaid-svg-vUZkeLPeN0IzzhlY .activeText2,#mermaid-svg-vUZkeLPeN0IzzhlY .activeText3{fill:#000 !important}#mermaid-svg-vUZkeLPeN0IzzhlY .done0,#mermaid-svg-vUZkeLPeN0IzzhlY .done1,#mermaid-svg-vUZkeLPeN0IzzhlY .done2,#mermaid-svg-vUZkeLPeN0IzzhlY .done3{stroke:grey;fill:#d3d3d3;stroke-width:2}#mermaid-svg-vUZkeLPeN0IzzhlY .doneText0,#mermaid-svg-vUZkeLPeN0IzzhlY .doneText1,#mermaid-svg-vUZkeLPeN0IzzhlY .doneText2,#mermaid-svg-vUZkeLPeN0IzzhlY .doneText3{fill:#000 !important}#mermaid-svg-vUZkeLPeN0IzzhlY .crit0,#mermaid-svg-vUZkeLPeN0IzzhlY .crit1,#mermaid-svg-vUZkeLPeN0IzzhlY .crit2,#mermaid-svg-vUZkeLPeN0IzzhlY .crit3{stroke:#f88;fill:red;stroke-width:2}#mermaid-svg-vUZkeLPeN0IzzhlY .activeCrit0,#mermaid-svg-vUZkeLPeN0IzzhlY .activeCrit1,#mermaid-svg-vUZkeLPeN0IzzhlY .activeCrit2,#mermaid-svg-vUZkeLPeN0IzzhlY .activeCrit3{stroke:#f88;fill:#bfc7ff;stroke-width:2}#mermaid-svg-vUZkeLPeN0IzzhlY .doneCrit0,#mermaid-svg-vUZkeLPeN0IzzhlY .doneCrit1,#mermaid-svg-vUZkeLPeN0IzzhlY .doneCrit2,#mermaid-svg-vUZkeLPeN0IzzhlY .doneCrit3{stroke:#f88;fill:#d3d3d3;stroke-width:2;cursor:pointer;shape-rendering:crispEdges}#mermaid-svg-vUZkeLPeN0IzzhlY .milestone{transform:rotate(45deg) scale(0.8, 0.8)}#mermaid-svg-vUZkeLPeN0IzzhlY .milestoneText{font-style:italic}#mermaid-svg-vUZkeLPeN0IzzhlY .doneCritText0,#mermaid-svg-vUZkeLPeN0IzzhlY .doneCritText1,#mermaid-svg-vUZkeLPeN0IzzhlY .doneCritText2,#mermaid-svg-vUZkeLPeN0IzzhlY .doneCritText3{fill:#000 !important}#mermaid-svg-vUZkeLPeN0IzzhlY .activeCritText0,#mermaid-svg-vUZkeLPeN0IzzhlY .activeCritText1,#mermaid-svg-vUZkeLPeN0IzzhlY .activeCritText2,#mermaid-svg-vUZkeLPeN0IzzhlY .activeCritText3{fill:#000 !important}#mermaid-svg-vUZkeLPeN0IzzhlY .titleText{text-anchor:middle;font-size:18px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vUZkeLPeN0IzzhlY g.classGroup text{fill:#9370db;stroke:none;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family);font-size:10px}#mermaid-svg-vUZkeLPeN0IzzhlY g.classGroup text .title{font-weight:bolder}#mermaid-svg-vUZkeLPeN0IzzhlY g.clickable{cursor:pointer}#mermaid-svg-vUZkeLPeN0IzzhlY g.classGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-vUZkeLPeN0IzzhlY g.classGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-vUZkeLPeN0IzzhlY .classLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.5}#mermaid-svg-vUZkeLPeN0IzzhlY .classLabel .label{fill:#9370db;font-size:10px}#mermaid-svg-vUZkeLPeN0IzzhlY .relation{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-vUZkeLPeN0IzzhlY .dashed-line{stroke-dasharray:3}#mermaid-svg-vUZkeLPeN0IzzhlY #compositionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vUZkeLPeN0IzzhlY #compositionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vUZkeLPeN0IzzhlY #aggregationStart{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-vUZkeLPeN0IzzhlY #aggregationEnd{fill:#ECECFF;stroke:#9370db;stroke-width:1}#mermaid-svg-vUZkeLPeN0IzzhlY #dependencyStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vUZkeLPeN0IzzhlY #dependencyEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vUZkeLPeN0IzzhlY #extensionStart{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vUZkeLPeN0IzzhlY #extensionEnd{fill:#9370db;stroke:#9370db;stroke-width:1}#mermaid-svg-vUZkeLPeN0IzzhlY .commit-id,#mermaid-svg-vUZkeLPeN0IzzhlY .commit-msg,#mermaid-svg-vUZkeLPeN0IzzhlY .branch-label{fill:lightgrey;color:lightgrey;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vUZkeLPeN0IzzhlY .pieTitleText{text-anchor:middle;font-size:25px;fill:#000;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vUZkeLPeN0IzzhlY .slice{font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vUZkeLPeN0IzzhlY g.stateGroup text{fill:#9370db;stroke:none;font-size:10px;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vUZkeLPeN0IzzhlY g.stateGroup text{fill:#9370db;fill:#333;stroke:none;font-size:10px}#mermaid-svg-vUZkeLPeN0IzzhlY g.statediagram-cluster .cluster-label text{fill:#333}#mermaid-svg-vUZkeLPeN0IzzhlY g.stateGroup .state-title{font-weight:bolder;fill:#000}#mermaid-svg-vUZkeLPeN0IzzhlY g.stateGroup rect{fill:#ECECFF;stroke:#9370db}#mermaid-svg-vUZkeLPeN0IzzhlY g.stateGroup line{stroke:#9370db;stroke-width:1}#mermaid-svg-vUZkeLPeN0IzzhlY .transition{stroke:#9370db;stroke-width:1;fill:none}#mermaid-svg-vUZkeLPeN0IzzhlY .stateGroup .composit{fill:white;border-bottom:1px}#mermaid-svg-vUZkeLPeN0IzzhlY .stateGroup .alt-composit{fill:#e0e0e0;border-bottom:1px}#mermaid-svg-vUZkeLPeN0IzzhlY .state-note{stroke:#aa3;fill:#fff5ad}#mermaid-svg-vUZkeLPeN0IzzhlY .state-note text{fill:black;stroke:none;font-size:10px}#mermaid-svg-vUZkeLPeN0IzzhlY .stateLabel .box{stroke:none;stroke-width:0;fill:#ECECFF;opacity:0.7}#mermaid-svg-vUZkeLPeN0IzzhlY .edgeLabel text{fill:#333}#mermaid-svg-vUZkeLPeN0IzzhlY .stateLabel text{fill:#000;font-size:10px;font-weight:bold;font-family:'trebuchet ms', verdana, arial;font-family:var(--mermaid-font-family)}#mermaid-svg-vUZkeLPeN0IzzhlY .node circle.state-start{fill:black;stroke:black}#mermaid-svg-vUZkeLPeN0IzzhlY .node circle.state-end{fill:black;stroke:white;stroke-width:1.5}#mermaid-svg-vUZkeLPeN0IzzhlY #statediagram-barbEnd{fill:#9370db}#mermaid-svg-vUZkeLPeN0IzzhlY .statediagram-cluster rect{fill:#ECECFF;stroke:#9370db;stroke-width:1px}#mermaid-svg-vUZkeLPeN0IzzhlY .statediagram-cluster rect.outer{rx:5px;ry:5px}#mermaid-svg-vUZkeLPeN0IzzhlY .statediagram-state .divider{stroke:#9370db}#mermaid-svg-vUZkeLPeN0IzzhlY .statediagram-state .title-state{rx:5px;ry:5px}#mermaid-svg-vUZkeLPeN0IzzhlY .statediagram-cluster.statediagram-cluster .inner{fill:white}#mermaid-svg-vUZkeLPeN0IzzhlY .statediagram-cluster.statediagram-cluster-alt .inner{fill:#e0e0e0}#mermaid-svg-vUZkeLPeN0IzzhlY .statediagram-cluster .inner{rx:0;ry:0}#mermaid-svg-vUZkeLPeN0IzzhlY .statediagram-state rect.basic{rx:5px;ry:5px}#mermaid-svg-vUZkeLPeN0IzzhlY .statediagram-state rect.divider{stroke-dasharray:10,10;fill:#efefef}#mermaid-svg-vUZkeLPeN0IzzhlY .note-edge{stroke-dasharray:5}#mermaid-svg-vUZkeLPeN0IzzhlY .statediagram-note rect{fill:#fff5ad;stroke:#aa3;stroke-width:1px;rx:0;ry:0}:root{--mermaid-font-family: '"trebuchet ms", verdana, arial';--mermaid-font-family: "Comic Sans MS", "Comic Sans", cursive}#mermaid-svg-vUZkeLPeN0IzzhlY .error-icon{fill:#522}#mermaid-svg-vUZkeLPeN0IzzhlY .error-text{fill:#522;stroke:#522}#mermaid-svg-vUZkeLPeN0IzzhlY .edge-thickness-normal{stroke-width:2px}#mermaid-svg-vUZkeLPeN0IzzhlY .edge-thickness-thick{stroke-width:3.5px}#mermaid-svg-vUZkeLPeN0IzzhlY .edge-pattern-solid{stroke-dasharray:0}#mermaid-svg-vUZkeLPeN0IzzhlY .edge-pattern-dashed{stroke-dasharray:3}#mermaid-svg-vUZkeLPeN0IzzhlY .edge-pattern-dotted{stroke-dasharray:2}#mermaid-svg-vUZkeLPeN0IzzhlY .marker{fill:#333}#mermaid-svg-vUZkeLPeN0IzzhlY .marker.cross{stroke:#333}:root { --mermaid-font-family: "trebuchet ms", verdana, arial;} #mermaid-svg-vUZkeLPeN0IzzhlY {color: rgba(0, 0, 0, 0.75);font: ;}

PromiseRejectionEvent
promise指向被驳回Promise
reason被驳回原因

一个特别有用的例子:当你使用 Node.js 时,有些依赖模块可能会有未被处理的 rejected promises,这些都会在运行时打印到控制台。你可以在自己的代码中捕捉这些信息,然后添加与 unhandledrejection 相应的处理函数来做分析和处理,或只是为了让你的输出更整洁。举例如下:(TODO c1111)(TODO c1111)(TODO c1111)

window.addEventListener("unhandledrejection", event => {/* 你可以在这里添加一些代码,以便检查event.promise 中的 promise 和event.reason 中的 rejection 原因 */event.preventDefault();
}, false);

调用 event 的 preventDefault() 方法是为了告诉 JavaScript 引擎当 Promise 被拒绝时不要执行默认操作,默认操作一般会包含把错误打印到控制台,Node 就是如此的。

理想情况下,在忽略这些事件之前,我们应该检查所有被拒绝的 Promise,来确认这不是代码中的 bug。

Promise是啥(下面都是我照办过来的,没有废话,也是我不会的)

Promise 对象用于表示一个异步操作的最终完成 (或失败)及其结果值。

一个 Promise 对象代表一个在这个 promise 被创建出来时不一定已知的值(就下面三种状态)。它让您能够把异步操作最终的成功返回值或者失败原因和相应的处理程序关联起来。 这样使得异步方法可以像同步方法那样返回值:异步方法并不会立即返回最终的值,而是会返回一个 promise,以便在未来某个时候把值交给使用者。

一个 Promise 必然处于以下几种状态之一:(主要记住英文)
待定(pending): 初始状态,既没有被兑现,也没有被拒绝。(没表态,干还是不干)
已兑现/满足(fulfilled): 意味着操作成功完成。(干)
已拒绝(rejected): 意味着操作失败。(不干)
不要和惰性求值混淆: 有一些语言中有惰性求值和延时计算的特性,它们也被称为“promises”,例如 Scheme。JavaScript 中的 promise 代表的是已经正在发生的进程, 而且可以通过回调函数实现链式调用。 如果您想对一个表达式进行惰性求值,就考虑一下使用无参数的"箭头函数": f = () =>表达式 来创建惰性求值的表达式,使用 f() 求值。(让我想想真么做惰性函数,英文的的慢慢看 js惰性函数)

注意: 如果一个 promise 已经被兑现(fulfilled)或被拒绝(rejected),那么我们也可以说它处于已敲定(settled)状态。您还会听到一个经常跟 promise 一起使用的术语:已决议(resolved),它表示 promise 已经处于已敲定(settled)状态,或者为了匹配另一个 promise 的状态被"锁定"了。Domenic Denicola 的 States and fates中有更多关于 promise 术语的细节可以供您参考。(settled有三种状态:1.fulfilled 2.rejected 3.resolved)

Promise的链式调用

const myPromise =(new Promise(myExecutorFunc)).then(handleFulfilledA,handleRejectedA).then(handleFulfilledB,handleRejectedB).then(handleFulfilledC,handleRejectedC);// 或者,这样可能会更好...const myPromise =(new Promise(myExecutorFunc)).then(handleFulfilledA).then(handleFulfilledB).then(handleFulfilledC).catch(handleRejectedAny);handleFulfilled(value)       { /*...*/; return nextValue;  }
handleRejection(reason)  { /*...*/; throw  nextReason; }
handleRejection(reason)  { /*...*/; return nextValue;  }

官方解释:
过早地处理被拒绝的 promise 会对之后 promise 的链式调用造成影响。不过有时候我们因为需要马上处理一个错误也只能这样做。(有关应对影响的技巧,请参见下面示例中的 throw -999 )另一方面,在没有迫切需要的情况下,可以在最后一个.catch() 语句时再进行错误处理,这种做法更加简单。

这两个函数的签名很简单,它们只接受一个任意类型的参数。这些函数由您(编程者)编写。这些函数的终止状态决定着链式调用中下一个promise的"已敲定 (settled)"状态是什么。任何不是 throw 的终止都会创建一个"已决议(resolved)"状态,而以 throw 终止则会创建一个"已拒绝"状态。

(情况一)被返回的 nextValue 可能是另一个promise对象,这种情况下这个promise会被动态地插入链式调用。
(情况二)当 .then() 中缺少能够返回 promise 对象的函数时,链式调用就直接继续进行下一环操作。
因此,链式调用可以在最后一个 .catch() 之前把所有的 handleRejection 都省略掉。类似地, .catch() 其实只是没有给 handleFulfilled 预留参数位置的 .then() 而已。(.catch只处理错误,就省略啦要处理函数的地方)

自己理解:(两个myPromise对比)下面的比上面的看上去更加简洁,有错误统一处理

TODO 未完待续

方法

Promise.all()

Promise.allSettled()

Promise.any()

Promise.prototype.catch()

Promise.prototype.finally()

Promise.race()

Promise.reject()

Promise.resolve()

Promise.prototype.then()

anyc function

await

javaScript---异步那些事(promise)(21/11/8)相关推荐

  1. JavaScript异步编程【中】 -- Promise 详细解析

    文章内容输出来源:拉勾教育 大前端高薪训练营 前言 在ES6中,新增加了一种异步编程的解决方案Promise,它是一种规范,是一套处理JavaScript异步的机制. Promise的含义 简单来说, ...

  2. JavaScript异步编程(1)- ECMAScript 6的Promise对象

    JavaScript的Callback机制深入人心.而ECMAScript的世界同样充斥的各种异步操作(异步IO.setTimeout等).异步和Callback的搭载很容易就衍生"回调金字 ...

  3. 【学习笔记】Part1·JavaScript·深度剖析-函数式编程与 JS 异步编程、手写 Promise(二、JavaScript 异步编程)

    [学习笔记]Part1·JavaScript·深度剖析-函数式编程与 JS 异步编程.手写 Promise(课前准备) [学习笔记]Part1·JavaScript·深度剖析-函数式编程与 JS 异步 ...

  4. JavaScript异步与Promise基本用法(resolve与reject)

    Promise解决的问题 相信每个前端都遇到过这样一个问题,当一个异步任务的执行需要依赖另一个异步任务的结果时,我们一般会将两个异步任务嵌套起来,这种情况发生一两次还可以忍,但是发生很多次之后,就形成 ...

  5. JavaScript 异步编程--Generator函数、async、await

    JavaScript 异步编程–Generator函数 Generator(生成器)是ES6标准引入的新的数据类型,其最大的特点就是可以交出函数的执行的控制权,即:通过yield关键字标明需要暂停的语 ...

  6. JavaScript异步精讲,让你更加明白Js的执行流程!

    JavaScript异步精讲,让你更加明白Js的执行流程! 问题点 什么是单线程,和异步有什么关系 什么是 event-loop jQuery的Deferred Promise 的基本使用和原理 as ...

  7. 细说JavaScript异步函数发展历程

    2019独角兽企业重金招聘Python工程师标准>>> < The Evolution of Asynchronous JavaScript >外文梳理了JavaScri ...

  8. php异步轮询如何实现,深入剖析JavaScript异步之事件轮询

    本篇文章给大家带来的内容是关于深入剖析JavsScript异步之事件轮询,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助. JavsScript 是一门单线程的编程语言,这就意味着一个时 ...

  9. JavaScript异步编程

    JavaScript异步编程 一.概述 1.单线程模型 2.同步任务和异步任务 3.任务队列和事件循环 4.异步操作的模式 回调函数 事件监听 发布/订阅 5.异步操作的流程控制 串行执行 并行执行 ...

  10. javascript异步中的回调

    同期异步系列文章推荐 谈一谈javascript异步 javascript异步与promise javascript异步之Promise.all().Promise.race().Promise.fi ...

最新文章

  1. 面试:高频面试题:如何保证缓存与数据库的双写一致性?
  2. 向李开复和四中校长提问:AI时代来临,孩子的教育需要什么改变?
  3. matlab多维数组、结构体数组
  4. 为什么Android项目mainactivity中有一个变量R_【Android开发入门教程】二.Android应用程序结构分析!...
  5. SQLServer查询死锁
  6. 【原创】C++变量作用域(三)
  7. Java内部类简介.
  8. android gradle + junit + jacoco 集成jenkins,sonar系统
  9. Python eval 函数 - Python零基础入门教程
  10. 计算机python是什么意思_系统学习python-1.1什么是计算机
  11. 幅度调制(AM调制、DSB(双边带)调制、SSB、VSB)
  12. SQL Server中的查询优化技术:基础
  13. java entitybuilder_Java MultipartEntityBuilder.setContentType方法代码示例
  14. 对 kubeadm 进行故障排查
  15. dlib实现人脸识别方法
  16. for根据ID去重_汽车ECU参数标定之配置Overlay RAM实现Qorivva MPC57xx系列MCU参数在线标定和代码重映射原理和方法详解...
  17. 使用Intrinsics优化
  18. opencv实战——图像矫正算法深入探讨
  19. Intel原厂固态SSD硬盘抢先评测
  20. COOX基础培训之DiagTool

热门文章

  1. 如何优化一个网站的完整方案-SEO
  2. 认识一下,JavaScript今年25岁啦
  3. SPSSPRO模型归纳整理
  4. 写的不错的家庭关系的文章,转自天涯。《2》
  5. java web 润乾报表教程_润乾报表 dashboard 分析
  6. Weka OneR 和 ZeroR 加深理解
  7. 关于手机模拟器的探索
  8. Mydrivers: 64国IT竞争力排名 中国仅第49
  9. 软件工程网络15个人作业3——案例分析(201521123107)
  10. Excel VBA 中有关使用 UBound + CurrentRegion 提示类型不匹配的问题及解决方案