[ERROR] Promise.all 정상 동작 안함

김택주·2023년 2월 23일

ERROR

목록 보기
1/13
const testPromises = [];
testPromises[testPromises.length] = 
() => promiseFunc(param1, param2);

Promise
.all(testPromises)
.then(res => {
	console.log('완료');
})
.catch(err => {
	console.log('에러');
})

위 코드를 실행 시 promise.all 에 등록된 프로미스들이 전부 실행된 후에
then이 실행되는게 아닌 바로 then이 실행되어 확인해보니

Promise.all 에는 Promise 를 반환하는 배열이 파라미터로 들어가야 하는데
testPromises 에는 일반 함수들이 들어가있어 바로 then이 실행된 경우

const testPromises = [];
testPromises[testPromises.length] = 
promiseFunc(param1, param2);

Promise
.all(testPromises)
.then(res => {
	console.log('완료');
})
.catch(err => {
	console.log('에러');
})

testPromises에 함수가 아닌 Promise를 반환하는 함수를 실행시킨 값을 그대로 넣어
정상동작 확인

아래는 ChatGPT 문의 답변

Promise.all() 메소드는 배열의 모든 프로미스가 fulfilled 상태가 될 때까지 기다린 다음, 모든 프로미스가 fulfilled 된 결과값을 배열로 반환합니다. 하지만 위 코드에서 createZipPromises 배열에는 함수가 담겨 있습니다. 따라서 Promise.all() 메소드는 함수가 아닌 일반적인 값으로 인식하게 됩니다.

0개의 댓글