defer()。promise和Promise之间的区别 [英] Difference between defer().promise and Promise
问题描述
我知道defer分离promises状态控制和进程,这里使用Q作为示例, Q.defer()。promise
和 Q.Promise
完全不同,为什么要这样设计?这两个承诺有什么区别
I know defer separates promises states control and process, here using Q as an example, the promise returned by Q.defer().promise
and Q.Promise
is totally different, why designing in this way? and what's the difference between these two "Promise"
提前致谢
PS:我现在在工作欢迎来到Promise图书馆,发行版和PRS: https://github.com/XiaomingJS/Xiaoming.Promise
PS: I'm currently working on a Promise library, issuses and PRS are welcomed: https://github.com/XiaomingJS/Xiaoming.Promise
推荐答案
嗯,这是关于承诺解决方案的来源。 Q和一堆其他库提供了两个API:
Well, this is about the promise resolution source. Q and a bunch of other libraries offer two APIs:
- 旧版
延期
API - 你在其中创建了一个延迟,你可以.resolve(value)
并且它有一个你可以退货的承诺。 - 承诺构造函数 - 这是一个现代API,您可以在其中从完成源创建承诺。
- The legacy
defer
API - in which you create a deferred that you can.resolve(value)
and it has a promise you can return. - The promise constructor - which is the modern API in which you create the promise from a completion source.
粗略地做:
var d = Q.defer();
setTimeout(function(){ d.resolve(); }, 1000);
return d.promise;
与以下相同:
return new Promise(function(resolve, reject){
setTimeout(resolve, 1000);
});
所以你可能会问
嗯,延迟API首先出现。这是其他语言处理它的方式,它是文章处理它的方式,也是人们首先使用它的方式 - 然而 - 这两种API之间存在着重要的区别。 promise构造函数是throw安全的。
Well, the defer API came first. It's how some other languages deal with it, it's how the papers deal with it and it's how people used it first - however - there is an important difference between the two APIs. The promise constructor is throw safe.
承诺抽象异常处理并且是安全的。如果你扔进一个承诺链,它会将该异常转换为拒绝,引用规范:
Promises abstract exception handling and are throw safe. If you throw inside a promise chain it will convert that exception into a rejection, quoting the spec:
如果onFulfilled或onRejected引发异常e,promise2必须以e作为理由拒绝
If either onFulfilled or onRejected throws an exception e, promise2 must be rejected with e as the reason
假设您正在从XHR请求中解析JSON:
Let's assume you're parsing JSON from an XHR request:
function get(){
var d = Q.defer();
if(cached) { // use cached version user edited in localStorage
d.resolve(JSON.parse(cached));
} else { // get from server
myCallbackApi('/foo', function(res){ d.resolve(res); });
}
}
现在,让我们看看promise构造函数版本:
Now, let's look at the promise constructor version:
function get(){
return new Promise(function(resolve, reject){
if(cached) { // use cached version user edited in localStorage
resolve(JSON.parse(cached));
} else { // get from server
myCallbackApi('/foo', resolve);
}
});
}
现在,假设您的服务器以某种方式向您发送了无效的JSON(或者用户编辑了它到了无效的状态)你缓存了它。
Now, assume somehow your server sent you invalid JSON (or the user edited it to an invalid state) and you cached it.
在延迟版本中 - 它会同步抛出。所以你必须一般防范它。在底部版本它没有。最高版本用法如下:
In the defer version - it throws synchronously. So you have to generally guard against it. In the bottom version it does not. The top version usage would look like:
try{
return get().catch(function(e){
return handleException(e); // can also just pass as function
});
} catch(e){
handleException(e);
}
在底部版本中 - 承诺构造函数将转换抛出
s拒绝所以它就足够了:
In the bottom version - the promise constructor will convert throw
s to rejections so it is enough to do:
return get().then(function(e){
return handleException(e);
});
防止一整类程序员错误发生。
Preventing a whole class of programmer errors from ever happening.
这篇关于defer()。promise和Promise之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!