一般情況下我們都會使用?`new Promise()`?來創建promise對象,但是除此之外我們也可以使用其他方法。
在這里,我們將會學習如何使用?[`Promise.resolve`](http://liubin.github.io/promises-book/#Promise.resolve)?和?[`Promise.reject`](http://liubin.github.io/promises-book/#Promise.reject)這兩個方法。
## 2.1.1\. new Promise的快捷方式
靜態方法[`Promise.resolve(value)`](http://liubin.github.io/promises-book/#Promise.resolve)?可以認為是?`new Promise()`?方法的快捷方式。
比如?`Promise.resolve(42);`?可以認為是以下代碼的語法糖。
~~~
new Promise(function(resolve){
resolve(42);
});
~~~
在這段代碼中的?`resolve(42);`?會讓這個promise對象立即進入確定(即resolved)狀態,并將?`42`?傳遞給后面then里所指定的?`onFulfilled`?函數。
方法?`Promise.resolve(value);`?的返回值也是一個promise對象,所以我們可以像下面那樣接著對其返回值進行?`.then`?調用。
~~~
Promise.resolve(42).then(function(value){
console.log(value);
});
~~~
[Promise.resolve](http://liubin.github.io/promises-book/#Promise.resolve)作為?`new Promise()`?的快捷方式,在進行promise對象的初始化或者編寫測試代碼的時候都非常方便。
## 2.1.2\. Thenable
`Promise.resolve`?方法另一個作用就是將?[thenable](http://liubin.github.io/promises-book/#Thenable)?對象轉換為promise對象。
> [ES6 Promises](http://liubin.github.io/promises-book/#es6-promises)里提到了[Thenable](http://liubin.github.io/promises-book/#Thenable)這個概念,簡單來說它就是一個非常類似promise的東西。
就像我們有時稱具有?`.length`?方法的非數組對象為Array like一樣,thenable指的是一個具有?`.then`?方法的對象。
這種將thenable對象轉換為promise對象的機制要求thenable對象所擁有的?`then`?方法應該和Promise所擁有的?`then`?方法具有同樣的功能和處理過程,在將thenable對象轉換為promise對象的時候,還會巧妙的利用thenable對象原來具有的?`then`?方法。
到底什么樣的對象能算是thenable的呢,最簡單的例子就是?[jQuery.ajax()](https://api.jquery.com/jQuery.ajax/),它的返回值就是thenable的。
因為`jQuery.ajax()`?的返回值是?[jqXHR Object](http://api.jquery.com/jQuery.ajax/#jqXHR)?對象,這個對象具有?`.then`?方法。
~~~
$.ajax('/json/comment.json');// => 擁有 `.then` 方法的對象
~~~
這個thenable的對象可以使用?`Promise.resolve`?來轉換為一個promise對象。
變成了promise對象的話,就能直接使用?`then`?或者?`catch`?等這些在?[ES6 Promises](http://liubin.github.io/promises-book/#es6-promises)里定義的方法了。
將thenable對象轉換promise對象
~~~
var promise = Promise.resolve($.ajax('/json/comment.json'));// => promise對象
promise.then(function(value){
console.log(value);
});
~~~
> jQuery和thenable
> [jQuery.ajax()](https://api.jquery.com/jQuery.ajax/)的返回值是一個具有?`.then`?方法的?[jqXHR Object](http://api.jquery.com/jQuery.ajax/#jqXHR)對象,這個對象繼承了來自?[Deferred Object](http://api.jquery.com/category/deferred-object/)?的方法和屬性。
> 但是Deferred Object并沒有遵循[Promises/A+](http://liubin.github.io/promises-book/#promises-aplus)或[ES6 Promises](http://liubin.github.io/promises-book/#es6-promises)標準,所以即使看上去這個對象轉換成了一個promise對象,但是會出現缺失部分信息的問題。
> 這個問題的根源在于jQuery的?[Deferred Object](http://api.jquery.com/category/deferred-object/)?的?`then`?方法機制與promise不同。
> 所以我們應該注意,即使一個對象具有?`.then`?方法,也不一定就能作為ES6 Promises對象使用。
> * [JavaScript Promises: There and back again - HTML5 Rocks](http://www.html5rocks.com/en/tutorials/es6/promises/#toc-lib-compatibility)
> * [You're Missing the Point of Promises](http://domenic.me/2012/10/14/youre-missing-the-point-of-promises/)
> * [https://twitter.com/hirano_y_aa/status/398851806383452160](https://twitter.com/hirano_y_aa/status/398851806383452160)
`Promise.resolve`?只使用了共通的方法?`then`?,提供了在不同的類庫之間進行promise對象互相轉換的功能。
這種轉換為thenable的功能在之前是通過使用?`Promise.cast`?來完成的,從它的名字我們也不難想象它的功能是什么。
除了在編寫使用Promise的類庫等軟件時需要對Thenable有所了解之外,通常作為end-user使用的時候,我們可能不會用到此功能。
> 我們會在后面第4章的[Promise.resolve和Thenable](http://liubin.github.io/promises-book/#resolve-thenable)中進行詳細的說明,介紹一下結合使用了Thenable和Promise.resolve的具體例子。
簡單總結一下?`Promise.resolve`?方法的話,可以認為它的作用就是將傳遞給它的參數填充(Fulfilled)到promise對象后并返回這個promise對象。
此外,Promise的很多處理內部也是使用了?`Promise.resolve`?算法將值轉換為promise對象后再進行處理的。
- 前言
- 第一章 - 什么是Promise
- 1.1. 什么是Promise
- 1.2. Promise簡介
- 1.3. 編寫Promise代碼
- 第二章 - 實戰Promise
- 2.1. Promise.resolve
- 2.2. Promise.reject
- 2.3. 專欄: Promise只能進行異步操作?
- 2.4. Promise#then
- 2.5. Promise#catch
- 2.6. 專欄: 每次調用then都會返回一個新創建的promise對象
- 2.7. Promise和數組
- 2.8. Promise.all
- 2.9. Promise.race
- 2.10. then or catch?
- 第三章 - Promise測試
- 3.1. 基本測試
- 3.2. Mocha對Promise的支持
- 3.3. 編寫可控測試(controllable tests)
- 第四章 - Advanced
- 4.1. Promise的實現類庫(Library)
- 4.2. Promise.resolve和Thenable
- 4.3. 使用reject而不是throw
- 4.4. Deferred和Promise
- 4.5. 使用Promise.race和delay取消XHR請求
- 4.6. 什么是 Promise.prototype.done ?
- 4.7. Promise和方法鏈(method chain)
- 4.8. 使用Promise進行順序(sequence)處理
- 第五章 - Promises API Reference
- 5.1. Promise#then
- 5.2. Promise#catch
- 5.3. Promise.resolve
- 5.4. Promise.reject
- 5.5. Promise.all
- 5.6. Promise.race
- 第六章 - 用語集
- 第七章 - 參考網站
- 第八章 - 關于作者
- 第九章 - 關于譯者