Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change the toAsync example in detail #176

Open
hg-pyun opened this issue Oct 28, 2022 · 1 comment
Open

Change the toAsync example in detail #176

hg-pyun opened this issue Oct 28, 2022 · 1 comment

Comments

@hg-pyun
Copy link
Collaborator

hg-pyun commented Oct 28, 2022

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.

@rojiwon123
Copy link
Contributor

rojiwon123 commented Jul 17, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants