从承诺返回然后()

编程入门 行业动态 更新时间:2024-10-24 01:55:27
本文介绍了从承诺返回然后()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

从承诺返回然后()

本文发布于:2023-10-25 08:13:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1526420.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!