返回承诺与返回未定义内部的未定义之间的区别 [英] Difference between returning a promise vs returning undefined inside a promise
问题描述
我不确定我是否了解这两种常见方案之间的区别.
说我们有这个
user.save().then(function(val){anotherPromise1(val);}).then(function(val){anotherPromise2(val);}).catch(function(err){});
与之相对:
user.save().then(function(val){返回anotherPromise1(val);}).then(function(val){返回anotherPromise2(val);}).catch(function(err){});
我知道这会有所作为,但是到底有什么作用?
如果您没有从 then
回调中返回值,则实际上是在返回 undefined
.下一个 then
回调将立即运行,并且将 undefined
作为分辨率值.
如果您从 then
回调返回承诺,则第二个 then
回调将等待该承诺(间接地,但这并不重要),以及何时承诺已解决,并从该承诺中获得了解决方案的价值.
(承诺/A +规范中的 then
规范对此进行了说明,但略有遗漏—并没有明确提及 onFulfilled
不返回任何内容会发生什么,但是在JavaScript中,调用函数 always 给出了您将得到一个结果值;如果该函数未显式返回某些内容,则调用它的结果为 undefined
.JavaScript没有 void
方法的概念C/C#/C ++/Java.)
您可以在此脚本中看到它 输出为(例如): 请注意无返回和有返回之间的区别: 没有,立即调用了 使用, I am not really sure I understand the difference between these two common scenarios. Say we have this: versus: I know it makes a difference but how exactly? If you don't return a value from the If you return a promise from the (This is covered by the You can see it in this script live copy on Babel's REPL: The output is (for example): Note the differences between without a return and with a return: Without, With, 这篇关于返回承诺与返回未定义内部的未定义之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!0015:anotherPromise [没有]得到420017:anotherPromise2 [without]不确定0018:全部完成0020:anotherPromise [with]得到421021年:anotherPromise2 [with]得到了841032:全部完成
otherPromise2
(从经过的时间值中可以看到),并且收到了 undefined
. anotherPromise2
等待 anotherPromise
的解析发生,然后收到 84
( otherPromise
的分辨率值)user.save().then(function(val){
anotherPromise1(val);
}).then(function(val){
anotherPromise2(val);
}).catch(function(err){
});
user.save().then(function(val){
return anotherPromise1(val);
}).then(function(val){
return anotherPromise2(val);
}).catch(function(err){
});
then
callback, you're effectively returning undefined
. The next then
callback will run immediately, and see undefined
as the resolution value.then
callback, the second then
callback waits on that promise (indirectly, but that doesn't really matter), and when that promise is resolved, gets the resolution value from that promise.then
specification in the Promises/A+ spec, but slightly by omission — it doesn't explicitly mention what should happen if onFulfilled
doesn't return anything, but in JavaScript, calling a function always gives you a resulting value; if the function doesn't explicitly return something, undefined
is the result of calling it. JavaScript doesn't have the concept of void
methods a'la C/C#/C++/Java.)let start = Date.now();
function elapsed() {
let rv = String(Date.now() - start);
while (rv.length < 4) {
rv = "0" + rv;
}
return rv;
}
function anotherPromise(type, val) {
console.log(`${elapsed()}: anotherPromise[${type}] got ${val}`);
return new Promise(resolve => {
setTimeout(() => { resolve(val * 2); }, 1000);
});
}
function anotherPromise2(type, val) {
console.log(`${elapsed()}: anotherPromise2[${type}] got ${val}`);
return new Promise(resolve => {
setTimeout(() => { resolve(val * 3); }, 10);
});
}
let user = {
save: () => {
return new Promise(resolve => {
setTimeout(() => {
resolve(42);
}, 10);
});
}
}
// Without return
user.save().then(function(val){
anotherPromise("without", val);
}).then(function(val){
anotherPromise2("without", val);
}).then(function() {
console.log(`${elapsed()}: All done`);
}).catch(function(err){
});
user.save().then(function(val){
return anotherPromise("with", val);
}).then(function(val){
return anotherPromise2("with", val);
}).then(function() {
console.log(`${elapsed()}: All done`);
}).catch(function(err){
});
0015: anotherPromise[without] got 42
0017: anotherPromise2[without] got undefined
0018: All done
0020: anotherPromise[with] got 42
1021: anotherPromise2[with] got 84
1032: All done
anotherPromise2
was called immediately (as we can see from the elapsed time values) and received undefined
.anotherPromise2
waited for anotherPromise
's resolution to occur, and then received 84
(anotherPromise
's resolution value)