使用异步等待不返回值

我使用以下代码,这是有效的!我能够获取(当时)点差中的数据,http200


Promise.all([

    axios({

      method: 'post',

      url: 'https://oauth2.-arch.mand.com/oauth2/token',

      data: qs.stringify({

        'grant_type': 'client_credentials'

      }, {

        'scope': 'run:crud'

      }),

      headers: {

        'Accept': 'application/json',

        'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',

        'Content-Type': 'application/x-www-form-urlencoded',

      }

    }),

    axios({

      method: 'post',

      url: 'https://oauth2.-arch.mand.com/oauth2/token',

      data: qs.stringify({

        'grant_type': 'client_credentials'

      }, {

        'scope': 'exe:crud'

      }),

      headers: {

        'Accept': 'application/json',

        'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',

        'Content-Type': 'application/x-www-form-urlencoded',

      }

    })

  ]).then(axios.spread((...responses: AxiosResponse[]) => {

      const aToken = responses[0];

      const bToken = responses[1];

    }).catch(function(error: any) {

      console.log("Error: " + error);

    });

现在我想用异步函数包装它,该函数返回两个响应(响应[0],响应[1])


我创建了这个函数


const FetchData = async (): Promise<{ run: string; app: string }> => {


let aToken:string;

let bToken:string;

Promise.all([

    axios({

      method: 'post',

      url: 'https://oauth2.-arch.mand.com/oauth2/token',

      data: qs.stringify({

        'grant_type': 'client_credentials'

      }, {

        'scope': 'run:crud'

      }),

      headers: {

        'Accept': 'application/json',

        'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',

        'Content-Type': 'application/x-www-form-urlencoded',

      }

    }),

   

问题是我在属性中得到了空值,同时在main函数内放置了一个断点,我看到我进入了控制台,在我实际从中获取值之前,任何想法如何采用代码从dispa运算符返回值然后调用控制台?val).then(axios.spread((


烙印99
浏览 142回答 3
3回答

慕村225694

您使用不当。您需要等待。解决这两个承诺后,响应数据将位于您从 中获取的每个响应对象内部的一个属性中。如果你需要整个响应对象,那么你可以解构它async-awaitPromise.alldataaxios下面是一个示例const FetchData = async (): Promise<{ run: string; app: string }> => {&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; const [response1, response2] = await Promise.all([&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; axios({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; axios({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; ]);&nbsp; &nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; run: response1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; app: response2&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp;} catch (error) {&nbsp; &nbsp; &nbsp; console.log(error);&nbsp; &nbsp;}};下面是一个从 json 占位符 API 的 2 个终结点获取数据的演示

喵喔喔

您需要更改返回语句的位置:更新了代码并进行了更改:const FetchData = (): Promise<{ run: string; app: string }> => {let aToken:string;let bToken:string;// Return the promise herereturn Promise.all([&nbsp; &nbsp; axios({&nbsp; &nbsp; &nbsp; method: 'post',&nbsp; &nbsp; &nbsp; url: 'https://oauth2.-arch.mand.com/oauth2/token',&nbsp; &nbsp; &nbsp; data: qs.stringify({&nbsp; &nbsp; &nbsp; &nbsp; 'grant_type': 'client_credentials'&nbsp; &nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; 'scope': 'run:crud'&nbsp; &nbsp; &nbsp; }),&nbsp; &nbsp; &nbsp; headers: {&nbsp; &nbsp; &nbsp; &nbsp; 'Accept': 'application/json',&nbsp; &nbsp; &nbsp; &nbsp; 'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',&nbsp; &nbsp; &nbsp; &nbsp; 'Content-Type': 'application/x-www-form-urlencoded',&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }),&nbsp; &nbsp; axios({&nbsp; &nbsp; &nbsp; method: 'post',&nbsp; &nbsp; &nbsp; url: 'https://oauth2.-arch.mand.com/oauth2/token',&nbsp; &nbsp; &nbsp; data: qs.stringify({&nbsp; &nbsp; &nbsp; &nbsp; 'grant_type': 'client_credentials'&nbsp; &nbsp; &nbsp; }, {&nbsp; &nbsp; &nbsp; &nbsp; 'scope': 'exe:crud'&nbsp; &nbsp; &nbsp; }),&nbsp; &nbsp; &nbsp; headers: {&nbsp; &nbsp; &nbsp; &nbsp; 'Accept': 'application/json',&nbsp; &nbsp; &nbsp; &nbsp; 'Authorization': 'Basic NTgwNjgtMDhhZTczOGNl',&nbsp; &nbsp; &nbsp; &nbsp; 'Content-Type': 'application/x-www-form-urlencoded',&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; })&nbsp; ]).then(axios.spread((...responses: AxiosResponse[]) => {&nbsp; &nbsp; &nbsp; aToken = responses[0];&nbsp; &nbsp; &nbsp; bToken = responses[1];&nbsp; &nbsp; &nbsp; // This return is for getting the data once the promise is resolved either with then or await&nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;run: aToken ,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;app: bToken&nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }).catch(function(error: any) {&nbsp; &nbsp; &nbsp; &nbsp;console.log("Error: " + error);&nbsp; &nbsp; &nbsp; &nbsp;return error;&nbsp; &nbsp; });)}由于您在承诺解决之前返回值,因此您不会获得任何数据。您需要返回 promise,然后您需要返回所需的数据,以便在调用函数中解析 promise 后获取。

尚方宝剑之说

const res = await Promist.all([...])return { run: res[0], app: res[1] }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript