async await map promise all from axios

Harry Jung·2023년 3월 14일
0

axios

목록 보기
1/1
  1. axios 에서 가져오는 데이터
  2. axios를 반복으로 2번이상 돌려야 함
  3. axios/async를 써야 함

기본문

var arr = [1, 2, 3, 4, 5];

var results: number[] = await Promise.all(arr.map(async (item): Promise<number> => {
    await callAsynchronousOperation(item);
    return item + 1;
}));

cargo4u에 적용함

const answer = await Promise.all(weightValuesArray.map(async(weightValue, i) => {
        const res = `https://sandbox.sendle.com/api/quote?pickup_address_line2=${pickup_address_line2}&pickup_suburb=${pickup_suburb}&pickup_postcode=${pickup_postcode}&delivery_address_line1=${delivery_address_line1}&delivery_address_line2=${delivery_address_line2}&delivery_suburb=${delivery_suburb}&delivery_postcode=${delivery_postcode}&weight_value=${weightValue}&weight_units=kg`
        const data = await axios.get(res, 
              {
                  method: "GET",
                  headers: {
                      'content-type': 'application/json',
                      "Accept": "application/json",
                  },
                  auth: {
                      username: "SANDBOX_info_happypost_com_a",
                      password: "sandbox_H65bX4rHxftCpcGV6qnKd6GK"
                  },
              })
              return data.data
              console.log('DATA', data.data)
              
              // setPrice( data?.data[0].quote.gross.amount * value.size[0].qty)
      })
      )
      console.log('ANSWER', answer)
profile
Dreamsoft

0개의 댓글