1. 程式人生 > 遊戲攻略 >《鬼泣巔峰之戰》第十三章刻爾柏洛斯機制及打法攻略

《鬼泣巔峰之戰》第十三章刻爾柏洛斯機制及打法攻略

概述

是非同步程式設計的一種解決方案。

從語法上說,Promise 是一個物件,從它可以獲取非同步操作的訊息。

狀態的特點

Promise 非同步操作有三種狀態:pending(進行中)、fulfilled(已成功)和 rejected(已失敗)。除了非同步操作的結果,任何其他操作都無法改變這個狀態。

Promise 物件只有:從 pending 變為 fulfilled 和從 pending 變為 rejected 的狀態改變。只要處於 fulfilled 和 rejected ,狀態就不會再變了即 resolved(已定型)。

狀態的缺點

無法取消 Promise ,一旦新建它就會立即執行,無法中途取消。

如果不設定回撥函式,Promise 內部丟擲的錯誤,不會反應到外部。

當處於 pending 狀態時,無法得知目前進展到哪一個階段(剛剛開始還是即將完成)。

then 方法

then 方法接收兩個函式作為引數,第一個引數是 Promise 執行成功時的回撥,第二個引數是 Promise 執行失敗時的回撥,兩個函式只會有一個被呼叫。

then 方法的特點

在 JavaScript 事件佇列的當前執行完成之前,回撥函式永遠不會被呼叫。

const p = new Promise(function(resolve,reject){
  resolve('success');
});
 
p.then(
function(value){ console.log(value); }); console.log('first'); // first
// success

通過.then形式新增的回撥函式,不論什麼時候,都會被呼叫。

通過多次呼叫.then,可以新增多個回撥函式,它們會按照插入順序並且獨立執行。

const p = new Promise(function(resolve,reject){
  resolve(1);
}).then(function(value){ // 第一個then // 1
  console.log(value);
  return value * 2;
}).then(
function(value){ // 第二個then // 2 console.log(value); }).then(function(value){ // 第三個then // undefined console.log(value); return Promise.resolve('resolve'); }).then(function(value){ // 第四個then // resolve console.log(value); return Promise.reject('reject'); }).then(function(value){ // 第五個then //reject:reject console.log('resolve:' + value); }, function(err) { console.log('reject:' + err); });

then 方法將返回一個 resolved 或 rejected 狀態的 Promise 物件用於鏈式呼叫,且 Promise 物件的值就是這個返回值

then 方法注意點

簡便的 Promise 鏈式程式設計最好保持扁平化,不要巢狀 Promise。

注意總是返回或終止 Promise 鏈。

const p1 = new Promise(function(resolve,reject){
  resolve(1);
}).then(function(result) {
  p2(result).then(newResult => p3(newResult));
}).then(() => p4());

建立新 Promise 但忘記返回它時,對應鏈條被打破,導致 p4 會與 p2 和 p3 同時進行。

大多數瀏覽器中不能終止的 Promise 鏈裡的 rejection,建議後面都跟上.catch(error => console.log(error));

Promise.prototype.then方法:鏈式操作

Promise.prototype.then 方法返回的是一個新的 Promise 物件,因此可以採用鏈式寫法。

getJSON("/posts.json").then(function(json) {
  return json.post;
}).then(function(post) {
  // proceed
});

上面的程式碼使用 then 方法,依次指定了兩個回撥函式。第一個回撥函式完成以後,會將返回結果作為引數,傳入第二個回撥函式。

如果前一個回撥函式返回的是Promise物件,這時後一個回撥函式就會等待該Promise物件有了執行結果,才會進一步呼叫。

getJSON("/post/1.json").then(function(post) {
  return getJSON(post.commentURL);
}).then(function(comments) {
  // 對comments進行處理
});

這種設計使得巢狀的非同步操作,可以被很容易得改寫,從回撥函式的"橫向發展"改為"向下發展"。

Promise.prototype.catch方法:捕捉錯誤

getJSON("/posts.json").then(function(posts) {
  // some code
}).catch(function(error) {
  // 處理前一個回撥函式執行時發生的錯誤
  console.log('發生錯誤!', error);
});

Promise 物件的錯誤具有"冒泡"性質,會一直向後傳遞,直到被捕獲為止。也就是說,錯誤總是會被下一個 catch 語句捕獲。

getJSON("/post/1.json").then(function(post) {
  return getJSON(post.commentURL);
}).then(function(comments) {
  // some code
}).catch(function(error) {
  // 處理前兩個回撥函式的錯誤
});

Promise 新建後立即執行,所以首先輸出的是Promise。然後,then方法指定的回撥函式,將在當前指令碼所有同步任務執行完才會執行