FxTS
FxTS copied to clipboard
Change the toAsync example in detail
If you look at this example, ([Promise list], to Async, map), I think it was a confusing point where logic should come forward asynchronously.
So I think it would be nice to change it to a more understandable example.
비동기 작업과 lazy 작업을 섞어서 사용하는 경우, 런타임에서, 미리 예상하기 어려운 오류가 발생할 수 있습니다.
"Error: 'Iterable' can not used with async function."
filter나 map에서 비동기 함수를 사용할 때, toAsync를 먼저 실행해야 정상적으로 작동합니다.
그래서 이에대한 설명과 예시도 추가하는게 좋겠습니다.
아래는 제 예시입니다.
const log = <T>(input: T) => console.log(input);
pipe(
0,
async () => 1,
() => Array.from({ length: 10 }, () => randomInt(100)),
toAsync, // toAsync가 filter나 map보다 나중에 나오면 에러가 발생합니다.
filter(async (num) => num > 0),
map(async (num) => num * 2),
toArray,
tap(log),
).then(console.log);
const log = <T>(input: T) => console.log(input);
pipe(
0,
async () => 1,
() => Array.from({ length: 10 }, () => randomInt(100)),
filter((num) => num > 0),
toAsync, // filter나 map의 실행 함수가 동기 작업인 경우, toAsync보다 먼저 나와도 상관 없음
map(async (num) => num * 2),
toArray,
tap(log),
).then(console.log);