FxTS icon indicating copy to clipboard operation
FxTS copied to clipboard

Change the toAsync example in detail

Open hg-pyun opened this issue 3 years ago • 1 comments

If you look at this example, ([Promise list], to Async, map), I think it was a confusing point where logic should come forward asynchronously.

image

So I think it would be nice to change it to a more understandable example.

hg-pyun avatar Oct 28 '22 03:10 hg-pyun

비동기 작업과 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);

rojiwon123 avatar Jul 17 '23 07:07 rojiwon123