How can I store the count of a for loop with promises that return in an arbitrary order?

Tom

I am making a series of http requests and I need to file the results into a list object when they return. I'm using angular promises.

Because the promises only resolve after the for loop is finished, they all get filed into the last index of list.

for (var i = 0;i < list.length; i+=1) {
    Promise.do(action).then(function(result) {
        list[i] //i is always at last index because the for loop has already completed
    }
}
dfsq

I would try to use $q.all for this:

var promises = [];

for (var i = 0; i < list.length; i += 1) {
    promises.push(Promise.do(action));
}

$q.all(promises).then(function(results) {
    console.log(results);
});

From the documentation:

Returns a single promise that will be resolved with an array/hash of values, each value corresponding to the promise at the same index/key in the promises array/hash. If any of the promises is resolved with a rejection, this resulting promise will be rejected with the same rejection value.

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

how can i loop for store items i want in ExtJS XTemplate?

분류에서Dev

MediaElement.js: How can I get percentage of time played (ej. 90%) in order count a complete visioned video

분류에서Dev

How can I save the order of the tree?

분류에서Dev

How can I move files into subdirectories by count?

분류에서Dev

How can I move files into subdirectories by count?

분류에서Dev

How can I return two values in a controller

분류에서Dev

How can I return a list of summary statistics?

분류에서Dev

how to i can return value in mvc 4

분류에서Dev

How can I break a symbolic link loop?

분류에서Dev

How can I iterate through a function with for loop?

분류에서Dev

How can I resolve this loop in c#?

분류에서Dev

How can I store a symbol in a Rails 4 model?

분류에서Dev

How can I store a part of an array to another array in matlab?

분류에서Dev

How can i store 2 numbers in a 1 byte char?

분류에서Dev

How can I pause a loop until I get a users input?

분류에서Dev

How can I copy and combine several .txt lists in alphabetical order?

분류에서Dev

How can I count files with a particular extension, and the directories they are in?

분류에서Dev

How can i count occurrence with few variables in R

분류에서Dev

How can I count data2.length with JavaScript?

분류에서Dev

How can I count the number of folders in a drive using Linux?

분류에서Dev

How can I get a count of files in a directory using the command line?

분류에서Dev

How can I increase ownership count of std::shared pointer

분류에서Dev

How can I create a table of only the unique elements in a list so I can order the elements in terms of frequency?

분류에서Dev

How can I get a consistent return type from this function?

분류에서Dev

How can i return a Django ModelForm through an Jquery Ajax call?

분류에서Dev

How can i insert return value to mysql database in python?

분류에서Dev

How can I return a response to an AngularJS $http POST to Sinatra?

분류에서Dev

How can I write the return of a function in javascript over an image in HTML?

분류에서Dev

How can I map keys in Insert mode that contains Carriage Return in it?

Related 관련 기사

  1. 1

    how can i loop for store items i want in ExtJS XTemplate?

  2. 2

    MediaElement.js: How can I get percentage of time played (ej. 90%) in order count a complete visioned video

  3. 3

    How can I save the order of the tree?

  4. 4

    How can I move files into subdirectories by count?

  5. 5

    How can I move files into subdirectories by count?

  6. 6

    How can I return two values in a controller

  7. 7

    How can I return a list of summary statistics?

  8. 8

    how to i can return value in mvc 4

  9. 9

    How can I break a symbolic link loop?

  10. 10

    How can I iterate through a function with for loop?

  11. 11

    How can I resolve this loop in c#?

  12. 12

    How can I store a symbol in a Rails 4 model?

  13. 13

    How can I store a part of an array to another array in matlab?

  14. 14

    How can i store 2 numbers in a 1 byte char?

  15. 15

    How can I pause a loop until I get a users input?

  16. 16

    How can I copy and combine several .txt lists in alphabetical order?

  17. 17

    How can I count files with a particular extension, and the directories they are in?

  18. 18

    How can i count occurrence with few variables in R

  19. 19

    How can I count data2.length with JavaScript?

  20. 20

    How can I count the number of folders in a drive using Linux?

  21. 21

    How can I get a count of files in a directory using the command line?

  22. 22

    How can I increase ownership count of std::shared pointer

  23. 23

    How can I create a table of only the unique elements in a list so I can order the elements in terms of frequency?

  24. 24

    How can I get a consistent return type from this function?

  25. 25

    How can i return a Django ModelForm through an Jquery Ajax call?

  26. 26

    How can i insert return value to mysql database in python?

  27. 27

    How can I return a response to an AngularJS $http POST to Sinatra?

  28. 28

    How can I write the return of a function in javascript over an image in HTML?

  29. 29

    How can I map keys in Insert mode that contains Carriage Return in it?

뜨겁다태그

보관