https://joshua1988.github.io/web-development/javascript/promise-for-beginners/#프로미스의-3가지-상태states

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Promise


for await … of

비동기 처리를 순차적으로 진행해야 할 때 사용한다. 순회하기 때문에 하나가 처리되어야 다음 처리로 넘어갈 수 있다.

for await (const item of items) {
	// do something...
}

Promise.all

모든 promise 객체들을 처리하여 배열로 받는 비동기 처리 방식이다.

for await … of 랑은 다른 점은 순차적으로 처리 하지 않는 다는 점이다. 때문에 promise.all은 promise 처리 시점이, 동시에 시작된다.

const result = await Promise.all(
	new Promise(...),
	new Promise(...),
	new Promise(...),
	new Promise(...),
	...
);

Promise.allSettled

모든 promise 객체들을 처리한다는 점은 all과 같다.

하지만 Promise.all에서 rejected되는 상태가 나온다면, fulfilled된 promise에 대한 결과를 볼 수 없다. (즉, 일부 성공한 작업도 실패처리됨)

하지만 allSettled는 rejected되었는지, fulfilled되었는지 상관없이 모든 settled된 promise 결과를 보여준다.

PromiseSettledResult {
	{ status: 'fullfiled', value: ? },
	{ status: 'rejected', reason: '...' },
	...
}
interface PromiseFulfilledResult<T> {
    status: "fulfilled";
    value: T;
}

interface PromiseRejectedResult {
    status: "rejected";
    reason: any;
}

type PromiseSettledResult<T> = PromiseFulfilledResult<T> | PromiseRejectedResult;