diff options
Diffstat (limited to 'files/zh-cn/mozilla/javascript_code_modules/promise.jsm/deferred/index.html')
| -rw-r--r-- | files/zh-cn/mozilla/javascript_code_modules/promise.jsm/deferred/index.html | 190 |
1 files changed, 190 insertions, 0 deletions
diff --git a/files/zh-cn/mozilla/javascript_code_modules/promise.jsm/deferred/index.html b/files/zh-cn/mozilla/javascript_code_modules/promise.jsm/deferred/index.html new file mode 100644 index 0000000000..aa0d921e0c --- /dev/null +++ b/files/zh-cn/mozilla/javascript_code_modules/promise.jsm/deferred/index.html @@ -0,0 +1,190 @@ +--- +title: Deferred +slug: Mozilla/JavaScript_code_modules/Promise.jsm/Deferred +translation_of: Mozilla/JavaScript_code_modules/Promise.jsm/Deferred +--- +<p> <code>Deferred</code> 对象返回了一个已经被废弃的方法<code>Promise.defer()</code> 并提供给新的promise用来改变promise的状态。</p> + +<p>从Gecko 30开始,该对象就已经被废弃,并建议不再使用。可以通过 <a href="/Mozilla/JavaScript_code_modules/Promise.jsm/Promise#Constructor" title="/Mozilla/JavaScript_code_modules/Promise.jsm/Promise#Constructor"><code>new Promise()</code></a> 构造新的promise取代,(或者使用向后/向前兼容的Deferred方法实现,如下:)。等价方法如下: </p> + +<pre class="brush: js">var deferred = Promise.defer(); +doSomething(function cb(good) { + if (good) + deferred.resolve(); + else + deferred.reject(); +}); +return deferred.promise;</pre> + +<p>应写成:</p> + +<pre class="brush: js">return new Promise(function(resolve, reject) { + doSomething(function cb(good) { + if (good) + resolve(); + else + reject(); + }); +});</pre> + +<h2 id="方法概览">方法概览</h2> + +<table class="standard-table"> + <tbody> + <tr> + <td><code>void <a href="#resolve()">resolve</a>([optional] aValue);</code></td> + </tr> + <tr> + <td><code>void <a href="#reject()">reject</a>([optional] aReason);</code></td> + </tr> + </tbody> +</table> + +<h2 id="属性">属性</h2> + +<table class="standard-table" style="width: auto;"> + <tbody> + <tr> + <td class="header">Attribute</td> + <td class="header">Type</td> + <td class="header">Description</td> + </tr> + <tr> + <td><code>promise</code> {{ReadOnlyInline()}}</td> + <td><a href="/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Promise" title="/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Promise"><code>Promise</code></a></td> + <td>新创建的promise,在pending阶段被初始化</td> + </tr> + </tbody> +</table> + +<h2 id="方法">方法</h2> + +<h3 id="resolve()">resolve()</h3> + +<p>将指定的值赋给新建的promise,或者改变已存在的 promise 状态。如果关联的 promise 已经调用过 resolve 方法,无论是传入值、rejection,或另一个 promise,此方法都不会起效果。</p> + +<div class="note"><strong>Note:</strong> This function is bound to its associated promise when <a href="https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm#defer()" title="https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm#defer()"><code>Promise.defer()</code></a> is called, and can be called with any value of <code>this</code>.</div> + +<div class="note"><strong>Note:</strong> Calling this method with a pending promise as the <code>aValue</code> argument, and then calling it again with another value before the promise is resolved or rejected, will have no effect the second time, as the associated promise is already resolved to the pending promise value as its resolution.</div> + +<pre>void resolve( + aValue +); +</pre> + +<h6 id="Parameters">Parameters</h6> + +<dl> + <dt><code>aValue</code> <span class="inlineIndicator optional optionalInline">Optional</span></dt> + <dd>If this value is not a promise, including <code>undefined</code>, it becomes the fulfillment value of the associated promise. If this value is a promise, then the associated promise will be resolved to the passed promise, and follow the state as the provided promise (including any future transitions).</dd> +</dl> + +<h3 id="reject()">reject()</h3> + +<p>Rejects the associated promise with the specified reason. If the promise has already been resolved, either to a value, a rejection, or another promise, this method does nothing.</p> + +<div class="note"><strong>Note:</strong> This function is bound to its associated promise when <a href="https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm#defer()" title="https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm#defer()"><code>Promise.defer()</code></a> is called, and can be called with any value of <code>this</code>.</div> + +<pre>void reject( + aReason +); +</pre> + +<h6 id="Parameters_2">Parameters</h6> + +<dl> + <dt><code>aReason</code> <span class="inlineIndicator optional optionalInline">Optional</span></dt> + <dd> + <p>The rejection reason for the associated promise. Although the reason can be <code>undefined</code>, it is generally an <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Error" title="/en-US/docs/JavaScript/Reference/Global_Objects/Error"><code>Error</code></a> object, like in exception handling.</p> + + <div class="note"><strong>Note:</strong> This argument should not be a promise. Specifying a rejected promise would make the rejection reason equal to the rejected promise itself, and not its rejection reason.</div> + </dd> +</dl> + +<h2 id="Backwards_and_forwards_compatible_helper">Backwards and forwards compatible helper<a id="backwards_forwards_compatible" name="backwards_forwards_compatible"></a></h2> + +<p>This Deferred function can be used for backwards and forwards compatibility, due to a change that took place in Firefox 30.</p> + +<pre>function Deferred() { + // update 062115 for typeof + if (typeof(Promise) != 'undefined' && Promise.defer) { + //need import of Promise.jsm for example: Cu.import('resource:/gree/modules/Promise.jsm'); + return Promise.defer(); + } else if (typeof(PromiseUtils) != 'undefined' && PromiseUtils.defer) { + //need import of PromiseUtils.jsm for example: Cu.import('resource:/gree/modules/PromiseUtils.jsm'); + return PromiseUtils.defer(); + } else { + /* A method to resolve the associated Promise with the value passed. + * If the promise is already settled it does nothing. + * + * @param {anything} value : This value is used to resolve the promise + * If the value is a Promise then the associated promise assumes the state + * of Promise passed as value. + */ + this.resolve = null; + + /* A method to reject the assocaited Promise with the value passed. + * If the promise is already settled it does nothing. + * + * @param {anything} reason: The reason for the rejection of the Promise. + * Generally its an Error object. If however a Promise is passed, then the Promise + * itself will be the reason for rejection no matter the state of the Promise. + */ + this.reject = null; + + /* A newly created Promise object. + * Initially in pending state. + */ + this.promise = new Promise(function(resolve, reject) { + this.resolve = resolve; + this.reject = reject; + }.bind(this)); + Object.freeze(this); + } +}</pre> + +<p>and use this function as would Promise.defer:</p> + +<pre class="brush: js">function funcWithDefer() { + var deferred = new Deferred(); + var promise = deferred.promise; + promise.then( + function(aVal) { + console.log('Fullfilled - ', aVal); + }, + function(aReason) { + console.error('Rejected - aReason:', aReason) + } + ); + + //deferred.resolve('aVal of resolved'); + deferred.reject('reject val'); + + return promise; +} + +var promise_funcWithDefer = funcWithDefer(); +promise_funcWithDefer.then( + function(aVal) { + console.log('Fullfilled - promise_funcWithDefer - ', aVal); + }, + function(aReason) { + var refObj = {name:'promise_funcWithDefer', aReason:aReason}; + console.error('Rejected - promise_funcWithDefer - ', refObj); + } +).catch( + function(aCatch) { + console.error('Caught - promise_funcWithDefer - ', aCatch); + throw aCatch; + } +);</pre> + +<h2 id="See_also">See also</h2> + +<ul> + <li><a href="https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm" title="/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm">Promise.jsm</a></li> +</ul> + +<p> + <audio style="display: none;"> </audio> +</p> |
