如何访问一个承诺的价值? [英] How to access the value of a promise?

查看:148
本文介绍了如何访问一个承诺的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待从角的文档这个例子 $ Q 但我认为这可能适用于承诺一般。他们有这样的例子:

I'm looking at this example from Angular's docs for $q but I think this probably applies to promises in general. They have this example:

promiseB = promiseA.then(function(result) {
  return result + 1;
});

// promiseB will be resolved immediately after promiseA is resolved and its value
// will be the result of promiseA incremented by 1

但我不清楚这是如何工作。如果我写。然后()上的结果,第一个。然后(),链接它们,这是我知道我可以,那么 promiseB 是一个承诺的对象类型,对象。这不是一个编号。那么他们怎么用,其价值将promiseA的加1的结果是什么意思?

But I'm not clear how this works. If I could write .then() on the result of the first .then(), chaining them, which I know I can, then promiseB is a promise object, of type Object. It is not a Number. So what do they mean by "its value will be the result of promiseA incremented by 1"?

我应该访问为 promiseB.value 或类似的东西?成功回调如何可以返回一个承诺并返回结果+ 1?我失去了一些东西。

Am I supposed to access that as promiseB.value or something like that? How can the success callback return a promise AND return "result + 1"? I'm missing something.

推荐答案

promiseA 然后函数返回一个新希望( promiseB ),它在 promiseA 立即解决的问题是解决了,它的价值是什么是返回的值从 promiseA 中成功的功能。

promiseA's then function returns a new promise (promiseB) that is immediately resolved after promiseA is resolved, its value is the value of the what is returned from the success function within promiseA.

在这种情况下, promiseA 与价值解析 - 结果,然后立即解析 promiseB 与值结果+ 1

In this case promiseA is resolved with a value - result and then immediately resolves promiseB with the value of result + 1.

访问 promiseB 在我们访问的结果 promiseA 以相同的方式完成的。

Accessing the value of promiseB is done in the same way we accessed the result of promiseA.

promiseB.then(function(result) {
    // here you can use the result of promiseB
});

这篇关于如何访问一个承诺的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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