将成功承诺解析的值分配给外部变量 [英] Assign value from successful promise resolve to external variable

查看:99
本文介绍了将成功承诺解析的值分配给外部变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常愚蠢的问题。请考虑以下事项:

I have a pretty silly problem. Consider the following:

vm.feed = getFeed().then(function(data) {return data;});

getFeed()返回$ q延期保证(我在角上)成功解决。

getFeed() returns a $q deferred promise (I am on angular) that resolves successfully.

我的目标是将vm.feed设置为等于成功回调返回的数据值。就像现在一样,代码只是将vm.feed赋予等于 getFeed()返回的 $ promise 对象。

My goal is to set vm.feed equal to the data value returned by the successful callback. As it is right now, the code simply assigns vm.feed equal to the $promise object returned by getFeed().

我知道我可以这样做: vm.feed = data 在已解决的函数中但我想了解为什么这段代码不能正常工作。

I know I could simply do: vm.feed = data inside the resolved function but I want to understand why this code does not work as it is.

PD:承诺正确解析,即使在解析后,vm.feed仍然等于Promise,而不是数据。我在+10秒后复制了console.log的vm.feed:

PD: the promise resolves correctly and even after it has been resolved vm.feed keeps being equal to the Promise, and not data. I copy the console.log of vm.feed after +10 seconds have elapsed:

Promise {$$state: Object} $$state: Objectstatus:1 value: Object

Promise对象中的value属性包含实际的解决方案我希望分配给vm.feed的承诺(ei 数据)。

That value property inside the Promise object contains the actual solution of the promise that I want to assign to vm.feed (e.i. data).

谢谢!

推荐答案

您的语句只是要求解释器分配从返回的值然后() vm.feed 变量。 然后()返回一个Promise(如你所见: https://github.com/angular/angular.js/blob/master/src/ng/q.js#L283 )。您可以通过看到Promise(一个简单的对象)被从函数中分配到 vm.feed 来描绘这一点。一旦解释器执行该行,就会发生这种情况。

Your statement does nothing more than ask the interpreter to assign the value returned from then() to the vm.feed variable. then() returns you a Promise (as you can see here: https://github.com/angular/angular.js/blob/master/src/ng/q.js#L283). You could picture this by seeing that the Promise (a simple object) is being pulled out of the function and getting assigned to vm.feed. This happens as soon as the interpreter executes that line.

因为当你调用然后()但是只有当你的诺言得到解决时(稍后,异步),然后()才能为调用者返回它的值。这是Javascript的默认方式。这就是Promise被引入的确切原因,所以你可以要求翻译以回调的形式推送给你的值。

Since your successful callback does not run when you call then() but only when your promise gets resolved (at a later time, asynchronously) it would be impossible for then() to return its value for the caller. This is the default way Javascript works. This was the exact reason Promises were introduced, so you could ask the interpreter to push the value to you, in the form of a callback.

虽然正在为JavaScript工作的未来版本(ES2016)中引入了几个关键字,但正如您现在所期望的那样。好消息是,您可以通过从ES2016转换到当前广泛支持的版本(ES5),开始编写此类代码。

Though on a future version that is being worked on for JavaScript (ES2016) a couple keywords will be introduced to work pretty much as you are expecting right now. The good news is you can start writing code like this today through transpilation from ES2016 to the current widely supported version (ES5).

有关该主题的精彩介绍,请参见: https://www.youtube.com/watch?v=lil4YCCXRYc

A nice introduction to the topic is available at: https://www.youtube.com/watch?v=lil4YCCXRYc

现在使用它可以通过Babel转换代码: https://babeljs.io/docs/usage/experimental/ (通过 - 第1阶段运行)。

To use it right now you can transpile your code through Babel: https://babeljs.io/docs/usage/experimental/ (by running with --stage 1).

您还可以在此处看到一些示例: https: //github.com/lukehoban/ecmascript-asyncawait

You can also see some examples here: https://github.com/lukehoban/ecmascript-asyncawait.

这篇关于将成功承诺解析的值分配给外部变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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