从承诺返回然后() [英] Return from a promise then()
问题描述
我有一个这样的javascript代码:
I have got a javascript code like this:
function justTesting() {
promise.then(function(output) {
return output + 1;
});
}
var test = justTesting();
var测试总是有一个未定义的值。我认为这是因为承诺没有得到解决。有没有办法从承诺中返回一个值?
I have got always an undefined value for the var test. I think that it is because the promises is not resolved jet..there is a way to return a value from a promises?
推荐答案
当你从then()回调中返回一些东西时,它有点神奇。如果返回一个值,则使用该值调用next then()。但是,如果你返回类似promise的东西,那么next()会等待它,并且只有在该promise承诺结束时才会被调用(成功/失败)。
When you return something from a then() callback, it's a bit magic. If you return a value, the next then() is called with that value. However, if you return something promise-like, the next then() waits on it, and is only called when that promise settles (succeeds/fails).
via < a href =https://developers.google.com/web/fundamentals/getting-started/primers/promises#queuing_asynchronous_actions\"rel =noreferrer> https://developers.google.com/web/fundamentals/getting-开始/ primer / promises#queuing_asynchronous_actions
这篇关于从承诺返回然后()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!