<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                https://es6.ruanyifeng.com/#docs/promise https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise ``` let?a?=?new?Promise((res,?rej)?=>?{ ????res('ok'); }).then(res?=>?{ ????console.log(res,?'then1'); }).then(res?=>?{ ????console.log(res,?'then2'); ????throw?Error; }).then(res?=>?{ ????console.log(res,?'then3'); }).finally(()?=>?{ ????console.log('finally1'); }).catch(err?=>?{ ????console.log(err,?'catch1'); ????return?err; }).then(res?=>?{ ????console.log(res,?'then4'); ????return?'4321'; }).finally(()?=>?{ ????console.log('finally2'); }).catch(err?=>?{ ????console.log(err,?'catch2'); }).then(res?=>?{ ????console.log(res,?'then5'); }); ``` ``` Promise.resolve("foobar") //?1.?接收?"foo"?并與?"bar"?拼接,并將其結果做為下一個?resolve?返回。 ??.then(function(string)?{ returnnewPromise(function(resolve,?reject)?{ setTimeout(function()?{ console.log('-------------------------') string?+=?'bar'; resolve(string); ??????},?1); ????}); ??}) //?2.?接收?"foobar",?放入一個異步函數中處理該字符串 //?并將其打印到控制臺中,?但是不將處理后的字符串返回到下一個。 ??.then(function(string)?{ console.log('\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*') setTimeout(function()?{ string?+=?'baz'; console.log(string); ????},?1) returnstring; ??}) //?3.?打印本節中代碼將如何運行的幫助消息, //?字符串實際上是由上一個回調函數之前的那塊異步代碼處理的。 ??.then(function(string)?{ console.log(string); console.log("Last?Then:??oops...?didn't?bother?to?instantiate?and?return?"?+ "a?promise?in?the?prior?then?so?the?sequence?may?be?a?bit?"?+ "surprising"); //?注意?`string`?這時不會存在?'baz'。 //?因為這是發生在我們通過setTimeout模擬的異步函數中。 console.log(string); console.log(333); ??}); ``` ## Promise 的含義 Promise 是異步編程的一種解決方案,比傳統的解決方案——回調函數和事件——更合理和更強大。它由社區最早提出和實現,ES6 將其寫進了語言標準,統一了用法,原生提供了`Promise`對象。 所謂`Promise`,簡單說就是一個容器,里面保存著某個未來才會結束的事件(通常是一個異步操作)的結果。從語法上說,Promise 是一個對象,從它可以獲取異步操作的消息。Promise 提供統一的 API,各種異步操作都可以用同樣的方法進行處理。 `Promise`對象有以下兩個特點。 (1)對象的狀態不受外界影響。`Promise`對象代表一個異步操作,有三種狀態:`pending`(進行中)、`fulfilled`(已成功)和`rejected`(已失敗)。**只有異步操作的結果,可以決定當前是哪一種狀態,任何其他操作都無法改變這個狀態。這也是`Promise`這個名字的由來,它的英語意思就是“承諾”,表示其他手段無法改變**。 (2)一旦狀態改變,就不會再變,任何時候都可以得到這個結果。`Promise`對象的狀態改變,只有兩種可能:從`pending`變為`fulfilled`和從`pending`變為`rejected`。只要這兩種情況發生,狀態就凝固了,不會再變了,會一直保持這個結果,這時就稱為 resolved(**已定型**)。如果改變已經發生了,你再對`Promise`對象添加回調函數,也會立即得到這個結果。這與事件(Event)完全不同,事件的特點是,如果你錯過了它,再去監聽,是得不到結果的。 注意,為了行文方便,本章后面的`resolved`統一只指`fulfilled`狀態,不包含`rejected`狀態。 有了`Promise`對象,就可以將異步操作以同步操作的流程表達出來,避免了層層嵌套的回調函數。此外,`Promise`對象提供統一的接口,使得控制異步操作更加容易。 `Promise`也有一些缺點。首先,無法取消`Promise`,一旦新建它就會立即執行,無法中途取消。其次,如果不設置回調函數,`Promise`內部拋出的錯誤,不會反應到外部。第三,當處于`pending`狀態時,無法得知目前進展到哪一個階段(剛剛開始還是即將完成)。 如果某些事件不斷地反復發生,一般來說,使用[Stream](https://nodejs.org/api/stream.html)模式是比部署`Promise`更好的選擇。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看