使用异步/等待等待或在打字稿中(反应本),但没有得到任何结果

问题描述 投票:0回答:1

在这里我打电话给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中没有解决。

请让我知道答案

javascript reactjs typescript react-native
1个回答
0
投票
我认为您需要从异步函数返回响应并进行一些代码更改:

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); }

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.