[英]Merge two arrays outside their functions using Promise.all
I'm trying to merge two arrays by their id (event.id) into one array. 我正在尝试通过其ID(event.id)将两个数组合并为一个数组。 I faced with a problem, I don't understand how to merge these arrays outside their functions.
我遇到了一个问题,我不明白如何在功能之外合并这些数组。 I know that there is a way to do it using
Promise.all
, but don't know how to do it. 我知道有一种方法可以使用
Promise.all
,但不知道如何进行。 Thank you for your time. 感谢您的时间。
function getpinnacle() {
return new Promise(function (resolve, reject) {
var options = {sportId: 29};
pinnacle.getFixtures(options, function(err, response, body) {
if (err) throw new Error(err);
var pinnFixtures = [];
body.league.forEach(function(leagues){
leagues.events.forEach(function(event){
if (event.status == 'O'){
pinnFixtures.push({
'id': event.id,
'homeTeamName': event.home,
'awayTeamName': event.away
});
};
});
});
resolve(pinnFixtures);
});
var options = {sportId: 29, oddsFormat: "DECIMAL"};
pinnacle.getOdds(options, function(err, response, body) {
if (err) throw new Error(err);
var pinnOdds = [];
body.leagues.forEach(function(league){
league.events.forEach(function(event){
event.periods.forEach(function(period){
if (period.moneyline !== undefined) {
pinnOdds.push({
'id': event.id,
'homeTeamOdds': period.moneyline.home,
'drawOdds': period.moneyline.draw,
'awayTeamOdds': period.moneyline.away
});
};
});
});
});
resolve(pinnOdds);
});
});
}
module.exports = getpinnacle;
In order to have a context where you have both arrays available to you, you can wrap each callback in a promise, and then use Promise.all()
to wait for the results: 为了使两个数组都可用,可以将每个回调包装在promise中,然后使用
Promise.all()
等待结果:
function getpinnacle() {
var options = {sportId: 29};
const fixtures = new Promise(function(resolve, reject){
pinnacle.getFixtures(options, function(err, response, body) {
if (err) throw new Error(err);
var pinnFixtures = [];
body.league.forEach(function(leagues){
leagues.events.forEach(function(event){
if (event.status == 'O'){
pinnFixtures.push({
'id': event.id,
'homeTeamName': event.home,
'awayTeamName': event.away
});
};
});
});
resolve(pinnFixtures);
});
});
var options = {sportId: 29, oddsFormat: "DECIMAL"};
const odds = new Promise(function(resolve, reject){
pinnacle.getOdds(options, function(err, response, body) {
if (err) throw new Error(err);
var pinnOdds = [];
body.leagues.forEach(function(league){
league.events.forEach(function(event){
event.periods.forEach(function(period){
if (period.moneyline !== undefined) {
pinnOdds.push({
'id': event.id,
'homeTeamOdds': period.moneyline.home,
'drawOdds': period.moneyline.draw,
'awayTeamOdds': period.moneyline.away
});
};
});
});
});
resolve(pinnOdds);
});
});
return Promise.all([fixtures, odds]);
}
After that you can call getPinnacle().then(function(results){...})
where results is an array [fixtures, odds]
. 之后,您可以调用
getPinnacle().then(function(results){...})
,其中result是一个数组[fixtures, odds]
。
What I would do to refactor your code is to break getFixtures
and getOdds
into two separate functions, each of which returns a promise. 我要重构代码的方法是将
getFixtures
和getOdds
分解为两个独立的函数,每个函数都返回一个promise。 Then getPinnacle would return Promise.all(getFixtures(), getOdds())
然后getPinnacle将
return Promise.all(getFixtures(), getOdds())
you may use promise.all like this 你可以这样使用promise.all
function getpinnacle() {
var promises = [];
var fixturePromise = new Promise( (resolve, reject) => {
///add your code here and resolve from inside you getFixture method
});
promises.push(fixturePromise);
var oddPromise = new Promise( (resolve, reject) => {
//add your getOdds method here and resolve this promise too
});
promise.push(oddPromise);
Promise.all(promises).then( (response) => {
var fixtureResponse = response[0];
var oddResponse = response[1];
//add your extra logic here
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.