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

[오혜성] 챕터 9: 비동기 프로그래밍 패턴 #70

Merged
merged 1 commit into from
Nov 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions 챕터_9/오혜성.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# 비동기 프로그래밍 패턴

> 저를 포함해 다 아시는 문법들을 알려주는 챕터였다고 생각됨

- 비동기 코드를 마치 동기 코드처럼 보이도록 작성할 수 있어 코드의 가독성과 이해도가 높아진다
- async/await 문법의 이유

> 어싱크 어웨잇의 처음은 F#이라는 언어라네요
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 처음 알았네용

> 이후에 C#, 파이썬, 타입스크립트, 자바스크립트 순으로 도입되었대요

## 비동기 반복

- for await of

```js
async function* repeat() {
yield 'Hello';
yield 'World';
}

async function main() {
for await (const word of repeat()) {
console.log(word);
}
}
```

## 재시도

```js
async function retry() {
let attempt = 0;

while (attempt < 3) {
try {
return await fetch('https://api.github.com');
} catch (err) {
attempt++;
}
}

throw new Error('Failed to fetch');
}
```

## 데코레이터

```js
const asyncLogger = (fn) => {
return async function(...args) {
console.log('Start: ', fn.name);
const result = await fn(...args);
console.log('End: ', fn.name);
return result;
};
};

const main = asyncLogger(async function main() {
return await new Promise(resolve => setTimeout(() => resolve('user'), 1000));
});

main();
```

> 책에서 나오는 `@asyncLogger` 데코레이터는 안됨 ㅋㅋㅋ 왜 적어둔거야
> > 자바스크립트에서의 데코레이터는 현재 스테이지 3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

> > https://github.com/tc39/proposal-decorator-metadata
Loading