从承诺返回然后() [英] Return from a promise then()

查看:105
本文介绍了从承诺返回然后()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆