JavaScript 中的 Deferred、Promise 和 Future 之间有什么区别? [英] What are the differences between Deferred, Promise and Future in JavaScript?

查看:16
本文介绍了JavaScript 中的 Deferred、Promise 和 Future 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Deferreds、Promises 和 Futures 之间有什么区别?
这三者背后是否有普遍认可的理论?

What are the differences between Deferreds, Promises and Futures?
Is there a generally approved theory behind all these three?

推荐答案

鉴于我显然不喜欢我试图回答 OP 问题的方式.字面的答案是,promise 是与其他对象共享的东西,而 deferred 应该保密.首先,延迟(通常扩展 Promise)可以自行解决,而 Promise 可能无法这样做.

In light of apparent dislike for how I've attempted to answer the OP's question. The literal answer is, a promise is something shared w/ other objects, while a deferred should be kept private. Primarily, a deferred (which generally extends Promise) can resolve itself, while a promise might not be able to do so.

如果您对细节感兴趣,请查看 Promises/A+.

If you're interested in the minutiae, then examine Promises/A+.

据我所知,首要目的是通过标准化接口提高清晰度和松散耦合.请参阅@jfriend00 的建议阅读:

So far as I'm aware, the overarching purpose is to improve clarity and loosen coupling through a standardized interface. See suggested reading from @jfriend00:

不是直接将回调传递给函数,而是可以导致紧密耦合的接口,使用承诺允许一个分别关注同步或异步代码.

Rather than directly passing callbacks to functions, something which can lead to tightly coupled interfaces, using promises allows one to separate concerns for code that is synchronous or asynchronous.

就个人而言,我发现 deferred 在处理例如由异步请求填充的模板,加载具有依赖关系网络的脚本,并以非阻塞方式提供用户反馈以形成数据.

Personally, I've found deferred especially useful when dealing with e.g. templates that are populated by asynchronous requests, loading scripts that have networks of dependencies, and providing user feedback to form data in a non-blocking manner.

确实,比较一下在JS模式下异步加载CodeMirror后做某事的纯回调形式(抱歉,我没有在while中使用jQuery):

Indeed, compare the pure callback form of doing something after loading CodeMirror in JS mode asynchronously (apologies, I've not used jQuery in a while):

/* assume getScript has signature like: function (path, callback, context) 
   and listens to onload && onreadystatechange */
$(function () {
   getScript('path/to/CodeMirror', getJSMode);

   // onreadystate is not reliable for callback args.
   function getJSMode() {
       getScript('path/to/CodeMirror/mode/javascript/javascript.js', 
           ourAwesomeScript);
   };

   function ourAwesomeScript() {
       console.log("CodeMirror is awesome, but I'm too impatient.");
   };
});

对于promises制定的版本(再次道歉,我在jQuery上不是最新的):

To the promises formulated version (again, apologies, I'm not up to date on jQuery):

/* Assume getScript returns a promise object */
$(function () {
   $.when(
       getScript('path/to/CodeMirror'),
       getScript('path/to/CodeMirror/mode/javascript/javascript.js')
   ).then(function () {
       console.log("CodeMirror is awesome, but I'm too impatient.");
   });
});

为半伪代码道歉,但我希望它能让核心思想变得清晰.基本上,通过返回标准化的承诺,您可以传递承诺,从而允许更清晰的分组.

Apologies for the semi-pseudo code, but I hope it makes the core idea somewhat clear. Basically, by returning a standardized promise, you can pass the promise around, thus allowing for more clear grouping.

这篇关于JavaScript 中的 Deferred、Promise 和 Future 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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