generated from muhandojeon/study-template
-
Notifications
You must be signed in to change notification settings - Fork 0
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
The head ref may contain hidden characters: "\uCC55\uD1309/\uC624\uD61C\uC131"
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# 비동기 프로그래밍 패턴 | ||
|
||
> 저를 포함해 다 아시는 문법들을 알려주는 챕터였다고 생각됨 | ||
|
||
- 비동기 코드를 마치 동기 코드처럼 보이도록 작성할 수 있어 코드의 가독성과 이해도가 높아진다 | ||
- async/await 문법의 이유 | ||
|
||
> 어싱크 어웨잇의 처음은 F#이라는 언어라네요 | ||
> 이후에 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
> > https://github.com/tc39/proposal-decorator-metadata |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오 처음 알았네용