{"id":19501326,"url":"https://github.com/self-denial-cy/event-loop-exercise","last_synced_at":"2025-08-20T15:04:14.928Z","repository":{"id":144544391,"uuid":"534480473","full_name":"self-denial-cy/event-loop-exercise","owner":"self-denial-cy","description":"Event Loop 事件循环机制代码实践","archived":false,"fork":false,"pushed_at":"2022-09-20T08:16:30.000Z","size":297,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-25T21:33:25.777Z","etag":null,"topics":["eventloop"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/self-denial-cy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-09T03:14:13.000Z","updated_at":"2022-12-07T10:35:53.000Z","dependencies_parsed_at":null,"dependency_job_id":"881c7757-aace-4f48-95bd-1af38f312a76","html_url":"https://github.com/self-denial-cy/event-loop-exercise","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/self-denial-cy/event-loop-exercise","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/self-denial-cy%2Fevent-loop-exercise","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/self-denial-cy%2Fevent-loop-exercise/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/self-denial-cy%2Fevent-loop-exercise/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/self-denial-cy%2Fevent-loop-exercise/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/self-denial-cy","download_url":"https://codeload.github.com/self-denial-cy/event-loop-exercise/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/self-denial-cy%2Fevent-loop-exercise/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271337910,"owners_count":24742057,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-20T02:00:09.606Z","response_time":69,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["eventloop"],"created_at":"2024-11-10T22:12:22.387Z","updated_at":"2025-08-20T15:04:14.877Z","avatar_url":"https://github.com/self-denial-cy.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# event-loop-exercise\n\nEvent Loop 事件循环机制代码实践\n\n## 进程与线程\n\n- 进程：一个程序相当于一个进程【浏览器打开一个页卡】\n- 线程：程序中具体做事的人\n\n## 同步与异步\n\n- 同步：如果做事的只有一个人，当前事未结束，下件事做不了【同步】\n- 异步：如果做事的有多个人，当前事未结束，下件事其他人可以去做【异步】\n\n\u003e 单线程只能同步，多线程可以异步\n\n## 浏览器是多线程的\n\n- GUI 渲染线程：渲染和解析页面（自上向下）\n- JS 引擎线程：渲染和解析 JS 【浏览器只会分配一个线程去解析 JS，所以 JS 本身是单线程的】\n- 定时器监听线程\n- 事件监听线程\n- HTTP 网络请求线程【同源下，浏览器最多分配 5~7 个 HTTP 线程（并发上限）】\n- Web Worker 线程\n- 等等\n\n\u003e JS 是单线程运行的，所以其中大部分代码都是同步执行的（例如：循环）\n\u003e\n\u003e 所以在 JS 中千万不要写死循环、死递归等操作，这些操作会一直占用 JS 引擎线程，导致后续的代码都无法执行\n\u003e\n\u003e 但是 JS 中也有部分异步执行的代码：异步微任务、异步宏任务\n\u003e\n\u003e JS 中的异步执行：借用浏览器的多线程机制，再基于 Event Loop 事件循环机制实现的单线程异步效果\n\n## 异步微任务【microtask】\n\n- requestAnimationFrame\n- Promise\n- async/await\n- queueMicrotask\n- MutationObserver\n- IntersectionObserver\n- process.nextTick【Node】\n\n## 异步宏任务【macrotask】\n\n- setTimeout/setInterval\n- 事件绑定/队列\n- XMLHttpRequest/Fetch\n- MessageChannel\n- setImmediate【Node】\n\n## exercise/2\n\n![exercise/2](./images/2.png)\n\n## exercise/7\n\n![exercise/7](./images/7.png)\n\n## exercise/8\n\n![exercise/8](./images/8.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fself-denial-cy%2Fevent-loop-exercise","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fself-denial-cy%2Fevent-loop-exercise","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fself-denial-cy%2Fevent-loop-exercise/lists"}