[英]Get Promise data out of it Javascript
I try this: 我尝试这样:
var result = [];
promise.then(function (data) {
result.push(data);
});
console.log(result)
and the result array is empty. 并且结果数组为空。 Is there a way to get it out of the promise?
有没有办法使它超出承诺?
No, you can't. 不, 你不能。
The point of promises is to allow a simple chaining of actions, some of them asynchronous. Promise的目的是允许简单地链接动作,其中一些动作是异步的。
You may do 你可以做
var result = [];
promise.then(function (data) {
result.push(data);
}).then(function(){
console.log(result)
});
I'd suggest you to read this introduction to promises . 我建议你阅读这份关于诺言的介绍 。
Promises are sometimes referred to as futures or future values . 承诺有时被称为期货或未来价值 。 You can't directly get the value out of the promise but you can get a promise of a future value , as seen below.
您不能直接从承诺中获得价值,但可以得到对未来价值的承诺,如下所示。
var futureResult = promise.then(function (data) {
var result = [];
result.push(data);
return result;
});
futureResult.then(function (result) {
console.log(result);
});
futureResult.then(function (result) {
console.log('another logger');
console.log(result);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.