Promise/A+ with chain then() 回调用例
Promise/A+ with chain then() callbacks use case?
我刚开始阅读有关Promise/A +的文章,并想亲自尝试一下。我添加了多个then()
回调,这种行为令人惊讶。
1)链接然后不会返回相同的承诺
> new Promise(function(a, r) {a('hello')}).
then(function(r) { console.log('1', arguments) }).
then(function(r) { console.log("2", arguments) })
1 ["hello"]
2 [undefined]
2)没有像我预期的那样链接工作
> p = new Promise(function(a, r) {a('hello')});
p.then(function(r) { console.log('1', arguments) });
p.then(function(r) { console.log("2", arguments) })
1 ["hello"]
2 ["hello"]
场景 #1 的用例是什么?
你只需要从承诺中返回值。
new Promise(function(a, r) {a('hello')}).
then(function(r) {
console.log('1', arguments);
return r;
}).
then(function(r) { console.log("2", arguments) })
返回的值作为参数传递给函数中的回调then
。
相关文章:
- 成功回调永远不会被JSONP请求调用
- 为什么(如何)'这'从函数内部调用回调时发生更改
- 如何在点击超链接时调用fullcalendar回调
- 在初始函数完成之前调用回调函数
- 未为路由器应用程序调用Node.Js的回调
- 对中的函数调用进行排序是回调的唯一方法
- Node.js exec调用从不调用回调
- 如何从参数数组中调用回调
- 当我的单元测试失败时,回调被调用了两次
- 回调函数(如afterMove)不被调用
- 如何使用jQuery等待来自回调的异步调用
- FireFox webrtc createoffer,没有任何回调调用
- 如何识别函数是作为方法调用还是作为回调调用
- jquery方法不是从angular回调调用的
- 从回调调用asyncwait
- 你可以使用PhoneGap和iOS从本地代码(而不是回调)调用javascript函数吗?
- Backbone model.fetch()回调(调用setTimeout)中的作用域问题
- React.js:你用一个不可调用的回调调用了' setState '
- 从图表.js选项回调调用 TypeScript 函数
- 如何将js函数从异步调用转换为回调调用