在这里我打电话给Axios呼叫,而Promise无法解决。 我在这里使用Typescript,typeScript的版本为4
getBalance = async (address: string) => {
let endpoints = this.getBalanceEndpoint();
for (let i = 0; i < endpoints.length; i++) {
let endpoint = endpoints[i];
const url = endpoint + address + '?details=basic';
const param: object = {
method: 'get',
url: url,
headers: {
'User-Agent': UA,
},
timeout: 1000,
};
try {
console.log(param,"Balance Params")
**const response = await axios(param)**;
console.log(response,"BITCOIN BALANCE")
}
} catch (e) {
console.log(e)
}
await new Promise(resolve =>
setTimeout(resolve, GENERIC_REQUEST_THROTTLE_DELAY)
);
}
throw new Error('Unable to retrieve balance!');
};
我没有得到任何结果或承诺在等待Axios中没有解决。
请让我知道答案
getBalance = async (address: string) => {
let endpoints = this.getBalanceEndpoint();
const result = []; // <-- create an RESULT variable
for (let i = 0; i < endpoints.length; i++) {
let endpoint = endpoints[i];
const url = endpoint + address + '?details=basic';
const param: object = {
method: 'get',
url: url,
headers: {
'User-Agent': UA,
},
timeout: 1000,
};
try {
console.log(param,"Balance Params")
const response = await axios(param); // <-- without **
console.log(response,"BITCOIN BALANCE");
result.push(response); // <-- add your response to result
} catch (e) {
console.log(e);
throw new Error('Unable to retrieve balance!'); //<-- here it should be if you want to create your own error
}
await new Promise(resolve =>
setTimeout(resolve, GENERIC_REQUEST_THROTTLE_DELAY)
);
}
return result; // <-- return result to get it from the outside
};
不要忘记从外部捕获错误
try {
const mResponses = await getBalance(mAddress);
} catch (e) {
console.log(e);
}