function createDataSet(username, region, champion, amount) {
var dataArray = []; //what I want to return, if possible with .map()
return getUserId(username, region) //required for getUserMatchlist()
.then(userId => {
getUserMatchlist(userId, region, champion, amount); //returns an array of objects
})
.then(matchlist => {
matchlist.forEach(match => {
getMatchDetails(match.gameId.toString(), region) //uses the Id from the matchlist objects to make another api request for each object
.then(res => {
dataArray.push(res); //every res is also an object fetched individually from the api.
// I would like to return an array with all the res objects in the order they appear in
})
.catch(err => console.log(err));
});
});
}
Я пытаюсь отправить данные, которые я получил, с нескольких apis на мой веб-интерфейс. Получение данных не является проблемой, однако, использование .map()
не сработало, и из того, что я прочитал, плохо работает с обещаниями. Как мне вернуть этот объект? (функция будет выполнена при получении запроса на получение и dataArray
будет отправлено обратно)