当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - how to assign the returned value of a promise to a variable?

问题描述:

This question already has an answer here:

  • How do I return the response from an asynchronous call?

    17 answers

网友答案:

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);
  });
分享给朋友:
您可能感兴趣的文章:
随机阅读: