c24165b8bc
에이전트 8종(대화형/프로그래머 C·S/디자이너/설계자/기획자/QA/PD/PM)에게 [공통 기본 능력 + 롤별 Specialty] 2층으로 지식을 주입하기 위한 재분류. 문서 내용·포맷은 무수정, 폴더 이동만 (6,372개 문서 수 보존 확인). - Topic_Programming → Domain_Programming (내부 구조 보존) - Topic_Graphic → Domain_Design - Topic_Business → Domain_Product - Topic_General → Domain_General - _Common 신설: Math(구 Topic_Math_Specialty), Reasoning(구 General/From_Thinking & Reasoning), Reasoning_Creativity(구 General/From_창의성), Communication(Poetic_Blog_Writing + From_writing) - 타 도메인의 From_* 폴더는 유지 (출처 표기일 뿐, 이미 도메인에 맞게 분류된 문서) - 빈 폴더 정리 (memory/procedures) - 에이전트→폴더 매핑은 workspace의 .astra/agent-knowledge-map.json (9개 에이전트) Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
5.9 KiB
5.9 KiB
id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
| id | title | category | status | canonical_id | aliases | duplicate_of | source_trust_level | confidence_score | verification_status | tags | raw_sources | last_reinforced | github_commit | tech_stack | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| wiki-2026-0508-javascript-async-and-event-loop | JavaScript Async and Event Loop | 10_Wiki/Topics | verified | self |
|
none | A | 0.9 | applied |
|
2026-05-10 | pending |
|
JavaScript Async and Event Loop
매 한 줄
"매 single-threaded JS 가 매 cooperative scheduler 위에서 매 async 를 흉내낸다.". Call stack + task queue + microtask queue + render pipeline 이 매 tick 의 단위. Promise/async-await 는 매 syntactic sugar — runtime 은 매 microtask drain rule 로 결정.
매 핵심
매 Stack vs Queues
- Call stack: 매 동기 frame. LIFO. 매 비어야 매 tick 진행.
- Task queue (macrotask): setTimeout, setInterval, MessageChannel, I/O callback, UI event. 매 tick 당 1개 drain.
- Microtask queue: Promise.then, queueMicrotask, MutationObserver. 매 tick 마지막에 매 전부 drain (재진입 포함).
- Animation frame queue: requestAnimationFrame. 매 paint 직전.
- Render steps: style → layout → paint → composite. 매 vsync 와 매 동기화.
매 Tick 순서 (browser)
- 매 task queue 에서 매 1 task pop → 실행.
- 매 microtask queue 매 전부 drain (recursively).
- 매 rAF callbacks.
- 매 render (필요 시).
- 매 idle callbacks (requestIdleCallback).
- 매 다음 tick.
매 Node.js phases
- timers → pending callbacks → idle/prepare → poll → check (setImmediate) → close → microtask drain (between phases since Node 11).
process.nextTick는 매 microtask 보다도 매 우선.
매 응용
- UI freeze 방지 — 매 long task 를 매 chunk + scheduler.yield().
- Race condition 분석 — 매 await 사이 매 state mutation 가능.
- Server backpressure — 매 event loop lag (
perf_hooks.monitorEventLoopDelay).
💻 패턴
Microtask vs Macrotask 순서
console.log('1');
setTimeout(() => console.log('2'), 0);
Promise.resolve().then(() => console.log('3'));
queueMicrotask(() => console.log('4'));
console.log('5');
// 1, 5, 3, 4, 2 — microtasks drain before next macrotask
Long task chunking with scheduler.yield (2026)
async function processLargeArray(items) {
for (let i = 0; i < items.length; i++) {
doWork(items[i]);
if (i % 100 === 0 && 'scheduler' in window) {
await scheduler.yield(); // Chrome 129+ stable
}
}
}
Promise.allSettled with concurrency limit
async function mapLimit(items, limit, fn) {
const results = new Array(items.length);
let cursor = 0;
const workers = Array.from({ length: limit }, async () => {
while (cursor < items.length) {
const i = cursor++;
results[i] = await fn(items[i]).catch(e => ({ error: e }));
}
});
await Promise.all(workers);
return results;
}
AbortController for cancellation
const ac = new AbortController();
fetch('/slow', { signal: ac.signal })
.then(r => r.json())
.catch(e => { if (e.name === 'AbortError') console.log('cancelled'); });
setTimeout(() => ac.abort(), 3000);
Async iterator drain
async function* readChunks(stream) {
const reader = stream.getReader();
try {
while (true) {
const { done, value } = await reader.read();
if (done) return;
yield value;
}
} finally { reader.releaseLock(); }
}
for await (const chunk of readChunks(resp.body)) {
process(chunk);
}
Avoid microtask starvation
// BAD — recursive Promise.resolve() blocks rendering
function bad() {
Promise.resolve().then(bad); // browser never paints
}
// GOOD — use setTimeout(0) or MessageChannel for yielding
function good() {
setTimeout(good, 0);
}
Event loop lag monitor (Node)
import { monitorEventLoopDelay } from 'node:perf_hooks';
const h = monitorEventLoopDelay({ resolution: 20 });
h.enable();
setInterval(() => {
console.log('p99 lag ms', h.percentile(99) / 1e6);
h.reset();
}, 1000);
Top-level await (ESM)
// module.mjs
const config = await fetch('/config.json').then(r => r.json());
export default config;
// importer awaits parent module graph — beware deadlock cycles
매 결정 기준
| 상황 | Approach |
|---|---|
| 매 즉시 yield 필요 | queueMicrotask / Promise.resolve().then |
| 매 paint 후 작업 | requestAnimationFrame |
| 매 idle 시간 작업 | requestIdleCallback / scheduler.postTask('background') |
| 매 long task chunking | scheduler.yield() (modern) |
| 매 cancellation | AbortController + signal |
| 매 backpressure 측정 | perf_hooks.monitorEventLoopDelay |
기본값: async/await + AbortController. 매 long task 는 매 scheduler.yield. 매 nextTick / process.nextTick 남용 X.
🔗 Graph
- 부모: JavaScript · Concurrency
- 변형: Browser Rendering
- 응용: React Concurrent Mode · Web Worker (웹 워커)
- Adjacent: AbortController · Streams
🤖 LLM 활용
언제: 매 race / ordering bug 분석, 매 long-task profiling, 매 SSR streaming 설계. 언제 X: 매 CPU-bound 작업 — 매 worker / native 로 offload.
❌ 안티패턴
- Recursive microtask loop: 매 rendering starvation.
- await in tight for loop: 매 직렬화. 매 Promise.all 사용.
- forgotten unhandled rejection: 매 process crash (Node 15+).
- setTimeout(fn, 0) for ordering: 매 microtask 와 매 race — queueMicrotask 사용.
🧪 검증 / 중복
- Verified (HTML spec — Event Loop Processing Model, Node.js docs).
- 신뢰도 A.
🕓 Changelog
| 날짜 | 변경 |
|---|---|
| 2026-05-08 | Phase 1 |
| 2026-05-10 | Manual cleanup — 매 task/microtask order + scheduler.yield 패턴 |