等待来自"然后"的退货声明角度$ q.all

时间:2017-06-07 22:34:57

标签: javascript angularjs

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

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

但我的功能并没有返回任何东西,因为它没有等待响应。另外,如果我必须从$ q.all内的$ q.all的then部分返回

,我该怎么办?

1 个答案:

答案 0 :(得分:3)

您可以从那里返回承诺本身和链.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
});

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