defer().promise 和 Promise 的区别 [英] Difference between defer().promise and Promise

查看:15
本文介绍了defer().promise 和 Promise 的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道defer将promise状态控制和进程分开,这里以Q为例,Q.defer().promiseQ.Promise返回的promise是完全不同,为什么要这样设计?以及这两个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/jiananshi/Promise-polyfill

PS: I'm currently working on a Promise library, issuses and PRS are welcomed: https://github.com/jiananshi/Promise-polyfill

推荐答案

嗯,这就是关于 promise 解析源.Q 和其他一些库提供了两个 API:

Well, this is about the promise resolution source. Q and a bunch of other libraries offer two APIs:

  • 遗留的 defer API - 在其中创建一个延迟,您可以 .resolve(value) 并且它承诺您可以返回.
  • promise 构造函数 - 这是现代 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);
});

所以你可能会问

好吧,defer API 是第一位的.这是其他一些语言如何处理它,这是论文如何处理它以及人们首先如何使用它 - 然而 - 这两种 API 之间存在重要区别.承诺构造函数是抛出安全的.

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.

Promise 抽象异常处理并且是安全抛出的.如果您在承诺链中抛出它,它将将该异常转换为拒绝,引用规范:

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);
}

在底层版本中——promise 构造函数会将 throws 转换为拒绝,所以这就足够了:

In the bottom version - the promise constructor will convert throws 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆