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

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

问题描述

我有一个非常愚蠢的问题.考虑以下几点:

I have a pretty silly problem. Consider the following:

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

getFeed() 返回一个成功解析的 $q 延迟承诺(我在 angular 上).

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:promise 正确解析,即使在解析后 vm.feed 仍然等于 Promise,而不是数据.我在 10 秒后复制 vm.feed 的 console.log:

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 的 Promise 的实际解决方案(即 data).

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).

谢谢!

推荐答案

您将得到 then() 返回的任何内容.但既然您正在阅读本文,以下内容可能会对您有所帮助:

You are going to get whatever then() returns. But since you are reading this, the following may help you:

您的语句只不过是要求解释器将 then() 返回的值分配给 vm.feed 变量.then() 返回一个 Promise(如您所见:https://github.com/angular/angular.js/blob/ce77c25b067b7b74d90de23bfb4aac6a27abb9d1/src/ng/q.js#L288).您可以通过看到 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/ce77c25b067b7b74d90de23bfb4aac6a27abb9d1/src/ng/q.js#L288). 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.

因为当你调用 then() 时你成功的回调不会运行,但只有当你的承诺得到解决时(稍后,异步),then() 为调用者返回其值.这是 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/(通过使用 --stage 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天全站免登陆