繁体   English   中英

等待来自角度$ q.all的“then”的return语句

[英]Wait for return statement from “then” of angular $q.all

我有一个函数,在函数内部,我有一个$ q.all调用,如下所示

function test()
{
    $q.all(promises).then(function(response)
    {
         return response;
    });
}

但是我的函数没有返回任何内容,因为它没有等待响应。 另外,如果我必须从$ q.all中的$ q.all的then部分返回,我该怎么办呢

你从那里返回承诺本身并链接.then处理程序。

function test() {
  return $q.all(promises);
}

test().then(function (response) {
  // do stuff with response
});

.then处理程序返回一个promise将它添加到外部promise链中,因此你可以像这样链接promises:

function test() {
  $q.all(promises).then(function (response) {
    return $q.all(morePromises);
  });
}

test().then(function (morePromisesResponse) {
  // do stuff
});

您也可以从.then处理程序返回一个非promise值,它包含在一个promise中并返回到外链,因此您可以在下一个.then处理程序中获取该值。

function test() {
  $q.all(promises).then(function (response) {
    return 123;
  });
}

test().then(function (result) {
  // result is 123
});

如果你仍然感到困惑,我很乐意给出一个更具体的答案。 我只需要您的代码示例以及您要完成的任务,以便我可以提供帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM