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

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

问题描述

延期,承诺和期货之间有什么区别?

这三种背后是否有普遍认可的理论?

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

推荐答案

明显不喜欢我试图回答OP的问题。字面上的答案是,承诺是与其他对象共享的东西,而延迟应该保密。主要是,延迟(通常扩展为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.

如果您对细枝末节感兴趣,请检查承诺/ 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:


不是直接将回调传递给函数,而是
可以导致紧密耦合的接口,使用promises允许一个
单独关注代码是同步的还是异步的。

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在处理eg时特别有用由异步请求填充的模板,加载具有依赖关系网络的脚本,以及以非阻塞方式提供用户反馈以形成数据。

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后做某事的回调形式(道歉,我没有在中使用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.");
   };
});

对承诺制定的版本(再次道歉,我不是最新的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天全站免登陆