Scikit Web

Javascript Fetch Promise All Request 예제 본문

ReactJS

Javascript Fetch Promise All Request 예제

Keun0 2022. 11. 1. 21:32
728x90
반응형

fetch 문 자체를 배열에 넣고 Promise all을 이용하여 한번에 실행 시키는 방법입니다.

 

response.json() 데이터를 맵에 넣고 

 

response.json 데이터를 넣은 맵을 await 로 한번더 해야 정상적으로 원하는 데이터를 얻을 수 있습니다.

 

async () => {
    let requestFetchAll = [] as any;
    requestFetchAll.push(
      fetch('https://google.com', {
        method: 'POST',
        headers: {
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({ test: 'test' }),
      }),
    );

    const preRequesetFetchAll = await Promise.all(requestFetchAll);
    const rtnPreFechAll = preRequesetFetchAll.map(async (response, idx) => {
      const respData = await response?.json();
      return respData;
    });
    
    const rtnFetchAll = await Promise.all(rtnPreFechAll);
    console.log(rtnFetchAll);
    
 };
728x90
반응형
Comments