Javascript 从承诺中获取数据而不是返回承诺

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26854654/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-22 23:27:17  来源:igfitidea点击:

Get data out of a promise instead of returning a promise

javascriptangularjspromise

提问by jrock2004

I am so sorry if the other promise threads have answered this but when looking at some of them I am just not getting the answer to solve my issue. I have three json files that I want to grab, parse and manually merge. The problem is I am getting stuck in promise jail. Let me show you some of the code from my angularjs controller.

如果其他承诺线程已经回答了这个问题,我很抱歉,但是在查看其中一些时,我只是没有得到解决我的问题的答案。我有三个要抓取、解析和手动合并的 json 文件。问题是我陷入了承诺监狱。让我向您展示一些来自我的 angularjs 控制器的代码。

$scope.tests = [];

$scope.tests = $http.get('results/testResults.json').then(function(res) {
  return res;
});

console.dir($scope.tests);

From the console.dir I am getting a promise but what I was hoping for was the data from the res variable. There has to be some way to get that data out. Is there no way to get that data out of the promise to a global variable so other promises of functions can use this data? Thanks

从 console.dir 我得到了一个承诺,但我希望的是来自 res 变量的数据。必须有某种方法来获取这些数据。有没有办法从对全局变量的承诺中获取该数据,以便其他函数承诺可以使用这些数据?谢谢

回答by jfriend00

The promise completes some time in the future. You are examining the promise variable before the data is in the promise. You should stay in the promise chain to use your data. Outside the promise chain, you don't know the timing of the asynchronous events (that's why you use promises in the first place).

承诺在未来的某个时间完成。在数据进入承诺之前,您正在检查承诺变量。您应该留在承诺链中以使用您的数据。在承诺链之外,您不知道异步事件的时间安排(这就是您首先使用承诺的原因)。

If you really don't want to use the data right in your first .then()handler which is the ideal place to use it, then you can chain another .then()onto your promise:

如果你真的不想在你的第一个.then()处理程序中使用数据,这是使用它的理想场所,那么你可以将另一个链接.then()到你的承诺上:

$scope.tests = $http.get('results/testResults.json');

$scope.tests.then(function(data) {
   // can use data here
});

FYI, promises do not populate data into global variables. They make the data available to use in .then()callbacks when those callbacks are called.

仅供参考,承诺不会将数据填充到全局变量中。.then()当这些回调被调用时,它们使数据可用于回调。

回答by user3898117

Make up an an property and stick it on the window object, like this,

编一个属性贴在window对象上,像这样,

window.myProperty

窗口.myProperty

Then pass your data to it, from inside the promise, like this,

然后将您的数据从承诺内部传递给它,就像这样,

window.myProperty = data;

window.myProperty = 数据;

And pick it up, when you are back outside, like this,

然后拿起它,当你回到外面时,像这样,

myVariable = window.myProperty;

myVariable = window.myProperty;

Or the reverse, because I cannot seem to get data in or out of some Promises, so that would go like this,

或者反过来,因为我似乎无法从某些 Promise 中获取数据,所以会像这样,

Again, make up a property and stick it on the window object, like this,

再补一个属性贴在window对象上,像这样,

window.myProperty

窗口.myProperty

Then pass your data to it, from outside the promise, like this,

然后将你的数据从承诺之外传递给它,就像这样,

window.myProperty = data;

window.myProperty = 数据;

And pick it up, when you are inside, like this,

把它捡起来,当你在里面的时候,像这样,

myVariable = window.myProperty;

myVariable = window.myProperty;

This cannot be the best way to do this! Does anyone know a better way?

这不可能是最好的方法!有人知道更好的方法吗?