簡體   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