Javascript 如何将承诺的返回值分配给变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32612877/
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
how to assign the returned value of a promise to a variable?
提问by user305883
EDITED as comment to duplicateI quote from: [How do I return the response from an asynchronous call?
编辑为重复我引用的评论:[如何从异步调用返回响应?
Promises are containers for future values. When the promise receives the value (it is resolved) or when it is cancelled (rejected), it notifies all of its "listeners" who want to access this value.
承诺是未来价值的容器。当 promise 收到值(已解决)或被取消(拒绝)时,它会通知所有想要访问此值的“侦听器”。
This question is about how to return the value contained in the promise. The answer was useful to me, because it clarified that it is not possible to returnthe value, rather to access the value withinthe promise function.
这个问题是关于如何返回包含在promise中的值。答案对我很有用,因为它澄清了不可能返回值,而是在promise 函数中访问该值。
Other useful sources about the subject, here:
关于该主题的其他有用资源,请点击此处:
- How do I return the response from an asynchronous call?
- http://www.html5rocks.com/en/tutorials/es6/promises/
- jQuery deferreds and promises - .then() vs .done().
Below the original question:
在原始问题下方:
Could you please help in understanding how to get the value from a promise and differences between these two examples ?
您能否帮助理解如何从承诺中获得价值以及这两个示例之间的差异?
//I have a simple ajax call like:
var fetch = function(start_node, end_node) {
var apiEndpoint = 'localhost/nodes/';
var loadurl = apiEndpoint+start_node+'/'+end_node;
return $.ajax({
url: loadurl,
type: 'GET',
dataType: 'json',
jsonpCallback: 'json'
});
};
// Then I processed results in something like:
var getResult = function(data) {
// do smtg with data
var result = {'myobject' : result_from_data}
return result
}
And finally I want to assign it results.
最后我想给它分配结果。
The following works, but I think it wastes the concept of the promise since result is assigned to a global variable declared before it:
以下工作,但我认为它浪费了承诺的概念,因为结果被分配给在它之前声明的全局变量:
var r;
fetch('val1','val2')
.then(function(data){
r = getResult(data);
})
Instead the following assigns the promise function to res
.
相反,以下将承诺函数分配给res
.
var res = fetch('val1','val2')
.done(function(data){
return getResult(data);
})
Could you clarify how to pass the resulting 'myobject'
to the variable res
, and not the promise itself?
您能否澄清如何将结果传递'myobject'
给变量res
,而不是承诺本身?
I also tried:
我也试过:
var res = $.when(fetch('val1','val2'))
.done(function(data){
return getResult(data);
})
but no success.
但没有成功。
采纳答案by Adam
You have to use the global variable trick, or accept use save-as-a-promise trick.
您必须使用全局变量技巧,或者接受使用另存为承诺技巧。
var getStuff = $.when(req1,req2).then(function(data1,data2) { return data1.concat(data2); });
//the variable getStuff is now a promise and any .then chained
//to it will have data1.concat(data2) passed to it as an argument
getStuff
.then(function(data1Data2) {
console.log(data1Data2);
});
//the next time you want to use it, you have to use the same promise-interface with .then
getStuff
.then(function(data1Data2) {
console.log(data1Data2);
});