ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# ES6 Promise 对象 ## 概述 是异步编程的一种解决方案。 从语法上说,Promise 是一个对象,从它可以获取异步操作的消息。 ## Promise 状态 ### 状态的特点 Promise 异步操作有三种状态:pending(进行中)、fulfilled(已成功)和 rejected(已失败)。除了异步操作的结果,任何其他操作都无法改变这个状态。 Promise 对象只有:从 pending 变为 fulfilled 和从 pending 变为 rejected 的状态改变。只要处于 fulfilled 和 rejected ,状态就不会再变了即 resolved(已定型)。 ``` <pre class="calibre13">``` <span class="token2">const</span> p1 <span class="token">=</span> <span class="token2">new</span> <span class="token3">Promise</span><span class="token1">(</span><span class="token2">function</span><span class="token1">(</span>resolve<span class="token1">,</span>reject<span class="token1">)</span><span class="token1">{</span> <span class="token3">resolve</span><span class="token1">(</span><span class="token4">'success1'</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">resolve</span><span class="token1">(</span><span class="token4">'success2'</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> <span class="token2">const</span> p2 <span class="token">=</span> <span class="token2">new</span> <span class="token3">Promise</span><span class="token1">(</span><span class="token2">function</span><span class="token1">(</span>resolve<span class="token1">,</span>reject<span class="token1">)</span><span class="token1">{</span> <span class="token3">resolve</span><span class="token1">(</span><span class="token4">'success3'</span><span class="token1">)</span><span class="token1">;</span> <span class="token3">reject</span><span class="token1">(</span><span class="token4">'reject'</span><span class="token1">)</span><span class="token1">;</span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> p1<span class="token1">.</span><span class="token3">then</span><span class="token1">(</span><span class="token2">function</span><span class="token1">(</span>value<span class="token1">)</span><span class="token1">{</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>value<span class="token1">)</span><span class="token1">;</span> <span class="token6">// success1 </span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> p2<span class="token1">.</span><span class="token3">then</span><span class="token1">(</span><span class="token2">function</span><span class="token1">(</span>value<span class="token1">)</span><span class="token1">{</span> console<span class="token1">.</span><span class="token3">log</span><span class="token1">(</span>value<span class="token1">)</span><span class="token1">;</span> <span class="token6">// success3 </span> <span class="token1">}</span><span class="token1">)</span><span class="token1">;</span> ``` ``` ### 状态的缺点 无法取消 Promise ,一旦新建它就会立即执行,无法中途取消。 如果不设置回调函数,Promise 内部抛出的错误,不会反应到外部。 当处于 pending 状态时,无法得知目前进展到哪一个阶段(刚刚开始还是即将完成)。 ## then 方法 then 方法接收两个函数作为参数,第一个参数是 Promise 执行成功时的回调,第二个参数是 Promise 执行失败时的回调,两个函数只会有一个被调用。 ![](https://img.kancloud.cn/66/33/6633afc1911f1e583df95c3d6583c0cb_759x410.png) ![](https://img.kancloud.cn/99/0e/990ebca224fac55e67fbb27286c9561b_810x467.png) ![](https://img.kancloud.cn/ed/fd/edfd1e1ab3785f7ba966307e335c7bf6_746x378.png)