![](/img/trans.png)
[英]Angular: how to wait for final result of method while mapping is waiting for multiple api calls?
[英]How to wait for the final result of a for loop with API calls?
这个循环将运行任意次数,我想在它们之后得到结果。 我尝试的任何事情(promisifying、async/await、嵌套函数等)似乎都是死路一条。 我不明白为什么我不能在 API 调用或我在这里制作的 function 上粘贴 a.then。 但我怀疑这个问题在我的理解中更为根本,因为我似乎什至无法获得返回的“数据”......同样无法等待 API 调用。 将其包装在 promise 中会丢失“数据”,并且使用内部的“for循环”将其拉出也不起作用。 这让我质疑我在 JS/实现其他人的 API 方面的整个进展。
const gotPeeps = () => {
challongeClient.tournaments.show({
id: tournamentURL,
callback: (err, data) => {
//return data //doesnt return "data" from gotPeeps?
for (const [key, value] of Object.entries(data.tournament.matches)) {
if (value.match.state === 'open') {
peepsList.push(value.match.player1Id, value.match.player2Id)
console.log(peepsList)
}}}})}
gotPeeps()
编辑评论:我试图在 for 循环完成后得到结果。 我所指的“循环”是数据 Object 上的“for of”。 “将代码放在循环之后但在回调内”不起作用。 在我未能解决此问题的一周中,我有一个先前的问题: 如何解决 Javascript 中的竞争条件? 这是整个事情,一些过去的版本被注释掉了:
const tournamentModel = require('../models/tournamentSchema')
require('dotenv').config()
const challonge = require('challonge');
module.exports = {
name: 'getmatches',
aliases: ['gm'],
cooldown: 0,
description: 'Get Challonge data into console.',
execute(message, args, cmd, client, Discord, profileData) {
let peep = ''
let peepsList = ''
const tournamentURL = 'TESTING_Just_Sign_Up_If_You_See_This850587786533011496'
const challongeClient = challonge.createClient({
apiKey: process.env.CHALLONGE_API_KEY,
})
const getPlayer = (playerXId) => {
return new Promise((resolve, reject) => {
challongeClient.participants.show({
id: tournamentURL,
participantId: playerXId,
callback: (err, data) => {
if (err) {
reject(err);
return;
}
peep = data.participant.misc
peepsList.push(peep)
console.log(peepsList)
console.log('RUNNING GET PLAYER', playerXId, playerIndexCount)
resolve(peepsList);
}
});
});
}
const peepsList = []
const matchList = []
const gotPeeps = () => {
challongeClient.tournaments.show({
id: tournamentURL,
include_participants: 1,
include_matches: 1,
callback: (err, data) => {
for (const [key, value] of Object.entries(data.tournament.matches)) {
if (value.match.state === 'open') {
peepsList.push(value.match.player1Id, value.match.player2Id)
console.log(peepsList)
}
}
}
/*// GET PLAYERS
getPlayer(value.match.player1Id)
.then(() => {
getPlayer(value.match.player2Id)
})
}
*/
}
)
}
gotPeeps()
}}
你可以让这个 function 返回一个 promise 并等待 function (只要你的async
函数是anC1C425274C17A94F14Z async1)
const gotPeeps = () => {
return new Promise((resolve, reject) => {
const peepsList = []; // declare the empty array to e filled
challongeClient.tournaments.show({
id: tournamentURL,
callback: (err, data) => {
for (const [key, value] of Object.entries(data.tournament.matches)) {
if (value.match.state === "open") {
peepsList.push(value.match.player1Id, value.match.player2Id);
}
}
resolve(peepsList); // resolve the promise with the filled array
// TODO: handle reject
},
});
})
};
(async () => {
try {
const result = await gotPeeps();
} catch (error) {
// TODO: handle error
}
})();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.