如何将一个承诺的返回值分配给一个变量? [英] how to assign the returned value of a promise to a variable?

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

问题描述

已编辑为要重复的注释 我引用了以下内容:[如何退货异步调用的响应?

EDITED as comment to duplicate I 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 return the value, rather to access the value within the promise function.

有关此主题的其他有用资源,在这里:

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

在原始问题下方:

能否请您帮助理解如何从承诺中获得价值以及这两个示例之间的区别?

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.

以下方法有效,但是我认为这浪费了Promise的概念,因为将结果分配给了在其之前声明的全局变量:

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);
})

相反,以下内容将promise函数分配给res.

Instead the following assigns the promise function to res.

var res = fetch('val1','val2')
.done(function(data){
  return getResult(data);
})

您能否阐明如何将结果'myobject'传递给变量res,而不是promise本身?

Could you clarify how to pass the resulting 'myobject' to the variable res, and not the promise itself?

我也尝试过:

var res = $.when(fetch('val1','val2'))
.done(function(data){
  return getResult(data);
})

但没有成功.

推荐答案

您必须使用全局变量 trick ,或接受使用save-as-a-promise技巧.

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);
  });

这篇关于如何将一个承诺的返回值分配给一个变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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