我有一个这样的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 developers.google/web/fundamentals/getting-开始/ primer / promises#queuing_asynchronous_actions
更多推荐
从承诺返回然后()
发布评论