第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用異步等待不返回值

使用異步等待不返回值

烙印99 2022-09-16 21:43:40
我使用以下代碼,這是有效的!我能夠獲?。ó?dāng)時(shí))點(diǎn)差中的數(shù)據(jù),http200Promise.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);    });現(xiàn)在我想用異步函數(shù)包裝它,該函數(shù)返回兩個(gè)響應(yīng)(響應(yīng)[0],響應(yīng)[1])我創(chuàng)建了這個(gè)函數(shù)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',      }    }),   問題是我在屬性中得到了空值,同時(shí)在main函數(shù)內(nèi)放置了一個(gè)斷點(diǎn),我看到我進(jìn)入了控制臺(tái),在我實(shí)際從中獲取值之前,任何想法如何采用代碼從dispa運(yùn)算符返回值然后調(diào)用控制臺(tái)?val).then(axios.spread((
查看完整描述

3 回答

?
慕村225694

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊

您使用不當(dāng)。您需要等待。解決這兩個(gè)承諾后,響應(yīng)數(shù)據(jù)將位于您從 中獲取的每個(gè)響應(yīng)對(duì)象內(nèi)部的一個(gè)屬性中。如果你需要整個(gè)響應(yīng)對(duì)象,那么你可以解構(gòu)它async-awaitPromise.alldataaxios


下面是一個(gè)示例


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

    try {

        const [response1, response2] = await Promise.all([

            axios({

               ...

            }),

            axios({

               ...

            })

        ]);


        return {

          run: response1,

          app: response2

        };


   } catch (error) {

      console.log(error);

   }

};

下面是一個(gè)從 json 占位符 API 的 2 個(gè)終結(jié)點(diǎn)獲取數(shù)據(jù)的演示


查看完整回答
反對(duì) 回復(fù) 2022-09-16
?
喵喔喔

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊

您需要更改返回語(yǔ)句的位置:


更新了代碼并進(jìn)行了更改:


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


let aToken:string;

let bToken:string;

// Return the promise here


return 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[]) => {

      aToken = responses[0];

      bToken = responses[1];


      // This return is for getting the data once the promise is resolved either with then or await

      return {

         run: aToken ,

         app: bToken

      };

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

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

       return error;

    });


)}

由于您在承諾解決之前返回值,因此您不會(huì)獲得任何數(shù)據(jù)。您需要返回 promise,然后您需要返回所需的數(shù)據(jù),以便在調(diào)用函數(shù)中解析 promise 后獲取。


查看完整回答
反對(duì) 回復(fù) 2022-09-16
?
尚方寶劍之說

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個(gè)贊

const res = await Promist.all([...])

return { run: res[0], app: res[1] }


查看完整回答
反對(duì) 回復(fù) 2022-09-16
  • 3 回答
  • 0 關(guān)注
  • 170 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)