延期、承诺和期货之间有什么区别?
这三个背后是否有一个普遍认可的理论?
这些答案,包括选定的答案,有利于引入承诺
从概念上讲,但缺乏具体差异的具体内容
使用库实现它们时出现的术语(并且有are重要区别)。
既然还是不断发展的规范 http://dom.spec.whatwg.org/#open-issues,目前的答案来自于尝试调查这两个参考文献(例如维基百科 http://en.wikipedia.org/wiki/Futures_and_promises)和实现(如jQuery http://api.jquery.com/category/deferred-object/):
-
Deferred: Never described in popular references,
1 http://en.wikipedia.org/wiki/Futures_and_promises
2 https://github.com/promises-aplus/promises-spec
3 http://dom.spec.whatwg.org/#promises
4 https://github.com/domenic/promises-unwrapping/blob/master/README.md
but commonly used by implementations as the arbiter of promise resolution (implementing resolve
and reject
).
5 http://dojotoolkit.org/reference-guide/1.9/dojo/Deferred.html
6 http://api.jquery.com/category/deferred-object/
7 https://github.com/kriskowal/q
Sometimes deferreds are also promises (implementing then
),
5 http://dojotoolkit.org/reference-guide/1.9/dojo/Deferred.html
6 http://api.jquery.com/category/deferred-object/
other times it's seen as more pure to have the Deferred only
capable of resolution, and forcing the user to access the promise for
using then
.
7 https://github.com/kriskowal/q
-
Promise:对正在讨论的战略最包罗万象的词。
A proxy object storing the result of a target function whose
synchronicity we would like to abstract, plus exposing a then
function
accepting another target function and returning a new promise.
2 https://github.com/promises-aplus/promises-spec
示例来自CommonJS http://wiki.commonjs.org/wiki/Promises/A:
> asyncComputeTheAnswerToEverything()
.then(addTwo)
.then(printResult);
44
Always described in popular references, although never specified as to
whose responsibility resolution falls to.
1 http://en.wikipedia.org/wiki/Futures_and_promises
2 https://github.com/promises-aplus/promises-spec
3 http://dom.spec.whatwg.org/#promises
4 https://github.com/domenic/promises-unwrapping/blob/master/README.md
Always present in popular implementations, and never given
resolution abilites.
5 http://dojotoolkit.org/reference-guide/1.9/dojo/Deferred.html
6 http://api.jquery.com/category/deferred-object/
7 https://github.com/kriskowal/q
-
Future: a seemingly deprecated term found in some popular references
1 http://en.wikipedia.org/wiki/Futures_and_promises
and at least one popular implementation,
8 https://github.com/FuturesJS/FuturesJS
but seemingly being phased out of discussion in preference for the term
'promise'
3 http://dom.spec.whatwg.org/#promises
and not always mentioned in popular introductions to the topic.
However, at least one library uses the term generically for abstracting
synchronicity and error handling, while not providing then
functionality.
10 https://github.com/angular/angular.js/blob/master/src/ngScenario/Future.js
It's unclear if avoiding the term 'promise' was intentional, but probably a
good choice since promises are built around 'thenables.'
2 https://github.com/promises-aplus/promises-spec
参考
- 关于承诺和期货的维基百科 http://en.wikipedia.org/wiki/Futures_and_promises
- 承诺/A+ 规格 http://promisesaplus.com/
- Promise 的 DOM 标准 http://dom.spec.whatwg.org/#promises
- DOM 标准 Promise 规范 WIP https://github.com/domenic/promises-unwrapping/blob/master/README.md
- DOJO 工具包延期 http://dojotoolkit.org/reference-guide/1.9/dojo/Deferred.html
- jQuery 延迟 http://api.jquery.com/category/deferred-object/
- Q https://github.com/kriskowal/q
- FutureJS https://github.com/FuturesJS/FuturesJS
- AngularJS 集成测试的未来 https://github.com/angular/angular.js/blob/master/src/ngScenario/Future.js
其他可能令人困惑的事情
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)