NetPax
NetPax

Reputation: 109

How to create array of value and corresponding Promise to pass to .then

I coded this:

  var promise = getHeaders(organisationUrl+tokenFirst);
    promise.then(function(reposPageNumber) {
      for (var i=1; i <= reposPageNumber; i++) {
        let orgReposUrl = organisationUrl+'?page='+i+tokenLast;
        orgReposUrlPromises.push(getData(orgReposUrl));
      }
      return Promise.all(orgReposUrlPromises)
    })
    .then(function(orgRepoData) {
      allOrgReposData = [].concat.apply([], orgRepoData);
      for (var j=0; j < allOrgReposData.length; j++) {
        let repoContributorsUrl = allOrgReposData[j].contributors_url;
        reposContributorsUrlPromises.push(getHeaders(repoContributorsUrl+tokenFirst));
      }
      return Promise.all(reposContributorsUrlPromises)
    })
    .then(function(repoContributorsData) {
      console.log(repoContributorsData);
    })
    .catch(function(error){
      console.log(error);
    });

In for loop marked by var "j" I get a number (getHeaders function) of subpages of url. When I return Promise.all to .then I've got a array of numbers without urls. The question is how to connect and pass an array of url&number of subpages to .then

Upvotes: 1

Views: 54

Answers (1)

Bless
Bless

Reputation: 5370

In the .then of the promise returned by getHeaders function, return the array of url & the no. of subpages (value resolved by the promise).

reposContributorsUrlPromises.push(
  getHeaders(repoContributorsUrl + tokenFirst)
    .then(function(result) {
      return [repoContributorsUrl + tokenFirst, result]
    })
)

Upvotes: 1

Related Questions