我刚刚开始阅读有关 Promise/A+ 的内容,并想亲自尝试一下。
我添加了多个 then()
回调,这种行为令人惊讶。
1) 链接 then 不会返回相同的 promise
> 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 的用例是什么?
最佳答案
您只需从 promise 中返回值即可。
new Promise(function(a, r) {a('hello')}).
then(function(r) {
console.log('1', arguments);
return r;
}).
then(function(r) { console.log("2", arguments) })
返回值作为参数传递给 then
函数中的回调。
关于javascript - Promise/A+ with chain then() 回调用例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35766947/