I am working on a Slack bot that fetches merge requests for given list of repositories (so multiple).
const fetchRepositories = (repositories) => {
return repositories.reduce(async (accumulator, currentRepository) => {
const eligibleMRs = await fetchEligibleMergeRequests(currentRepository.api);
// the PROBLEM is here
const refinedMRs = eligibleMRs.length && eligibleMRs.map(mr => {
// some logic to clean the shorten info received
}
return Promise.resolve(accumulator)
.then(...) //return personalized object for each repository
}, []);
};
The problem is that eligibleMRs is an array of Promise { <pending> } (each merge request).
And this problem appeared when instead of only fetching all repository merge requests, I added another .then that fetches the list of reviewers for every mr.
const fetchEligibleMergeRequests = (url) => {
return fetch(url, fetchOptions)
.then(response => {
return response.json();
})
.then(data => {
return data.filter(mr => filterMergeRequest(mr));
// when I only had this .then everything was perfect
})
.then(async filteredMRs => {
return filteredMRs.map(async mr => {
const fetchedReviewers = await fetchReviewers(mr["project_id"], mr.iid);
//this fetch is another call to GitlabAPI in order to retrieve reviewers
return Object.assign({}, mr, {
reviewers: fetchedReviewers
});
});
})
};
What I would like to ask help for is the fact that the newly added fetch (the one that gets the reviewers for each mr) seems to hold everything back. And I would like to make the main code TO WAIT for reviewers fetching.
Thanks to all trying to help.