{"id":16428107,"url":"https://github.com/wangshijun/course-javascript-async-await","last_synced_at":"2025-03-21T04:30:50.900Z","repository":{"id":145468079,"uuid":"106994690","full_name":"wangshijun/course-javascript-async-await","owner":"wangshijun","description":":fire: Source code for course \"asynchronous javascript with async/await\"","archived":false,"fork":false,"pushed_at":"2017-11-10T10:41:08.000Z","size":210,"stargazers_count":79,"open_issues_count":11,"forks_count":10,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-17T20:44:02.986Z","etag":null,"topics":["async","async-await","course","feweekly","javascript","nodejs"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wangshijun.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2017-10-15T08:10:31.000Z","updated_at":"2024-11-14T09:22:52.000Z","dependencies_parsed_at":null,"dependency_job_id":"85c7e474-ee6e-4c04-9809-6c42ad556f0c","html_url":"https://github.com/wangshijun/course-javascript-async-await","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wangshijun%2Fcourse-javascript-async-await","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wangshijun%2Fcourse-javascript-async-await/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wangshijun%2Fcourse-javascript-async-await/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wangshijun%2Fcourse-javascript-async-await/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wangshijun","download_url":"https://codeload.github.com/wangshijun/course-javascript-async-await/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244738220,"owners_count":20501803,"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","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":["async","async-await","course","feweekly","javascript","nodejs"],"created_at":"2024-10-11T08:15:07.953Z","updated_at":"2025-03-21T04:30:50.895Z","avatar_url":"https://github.com/wangshijun.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 玩转异步 JS ：async/await 简明教程\n\n## 课程介绍\n\n在软件开发领域，简洁的代码 =\u003e 容易阅读的代码 =\u003e 容易维护的代码，而 ES2017 中的 async/await 特性能让我们编写出相比回调地狱和 Promise 链式调用更直观、更容易理解的代码，await 关键字接收一个 Promise，等待代码执行，直到 Promise 状态变为 resolved 或者 rejected，这种特性能让我们的异步代码阅读起来更像是同步代码。\n\n本课程将会从编写简单的 async/await 函数开始，用实例带领大家探索 async/await 实战的方方面面，比如如何结合 Promise 和 await 关键字？如何定义不同类型的 async 函数？如何安全的使用 async/await，即处理错误？如何让多个 await 串行或并行？如何在循环中正确使用 await？\n\n## 适用人群\n\n* [required] 具备 JS 基础知识，知道如何运行 Node.js 脚本；\n* [required] 知道 Callback、Promise 等异步处理概念；\n* [optional] 期望学习新的异步特性，编写更简洁易懂易维护的代码；\n* [optional] 期望不断打磨自己的 JS 技能，让自己变得更值钱；\n\n## 内容目录\n\n### 1. 编写第一个 async/await 函数\n\n手把手教你把发起 HTTP 请求并解析响应的代码改写成 async/await 风格，让你学会 async/await 的基本语法。\n\n### 2. 将 async 函数用在 Promise 链中\n\n带你探索如何在 Promise 链中像使用其他 Promise 一样无缝使用 async 函数。\n\n### 3. 把任意类型的函数转成 async 风格\n\n实例演示如何将任意函数写成 async 风格，包括函数声明、函数表达式、箭头函数、类方法、对象方法等。\n\n### 4. 处理 async 函数中的错误\n\n不同于 Promise 中的 .catch() 错误处理机制，在 async 函数中，我们需要使用 try/catch 结构来恰当的处理错误。\n\n### 5. 正确处理多个 await 操作的并行串行\n\n学会通过移动 await 关键词的位置实现多个 await 操作串行或者并行，并且用数据证明让多个异步操作并行的性能优势。\n\n### 6. 使用 Promise.all() 让多个 await 操作并行\n\n学会使用 ES2015 中的解构和 Promise.all() 实现多个异步操作的并行，非常适合需要同时发起多个请求的情形，代码可读性不打折扣。\n\n### 7. 结合 await 和任意兼容 .then() 的代码\n\nawait 操作符并不仅仅局限于 ES2015 的 Promise，可以和任意兼容 .then() 方法的代码使用，通过实例学会如何与流行的 Promise 库结合使用。\n\n### 8. 在 for 循环中正确的使用 await\n\n理解 for 循环中 await 不同写法带来的性能影响，以及改进的方法。\n\n## 源码链接\n\n代码仓库：[wangshijun/course-javascript-async-await](https://github.com/wangshijun/course-javascript-async-await)\n\n运行方法（请确保系统中安装了 [Node.js](https://nodejs.org/en/)）：\n\n```shell\ngit clone https://github.com/wangshijun/course-javascript-async-await.git\ncd course-javascript-async-await\nnode xxx.js\n```\n\n上面的 xxx.js 对应每节课程的源码，列表如下：\n\n1. [编写第一个 async/await 函数](https://github.com/wangshijun/course-javascript-async-await/blob/master/01-first-async-await-function.js)\n1. [将 async 函数用在 Promise chain 中](https://github.com/wangshijun/course-javascript-async-await/blob/master/02-use-async-function-as-promise.js)\n1. [把任意类型的函数转成 async 风格](https://github.com/wangshijun/course-javascript-async-await/blob/master/03-convert-any-function-into-async.js)\n1. [处理 async 函数中的错误](https://github.com/wangshijun/course-javascript-async-await/blob/master/04-handling-errors-for-async.js)\n1. [正确处理多个 await 操作的并行串行](https://github.com/wangshijun/course-javascript-async-await/blob/master/05-await-multiple-promise-seq-or-concurrently.js)\n1. [使用 Promise.all() 让多个 await 操作并行](https://github.com/wangshijun/course-javascript-async-await/blob/master/06-await-multiple-promise-with-all.js)\n1. [结合 await 和任意兼容 .then() 的代码](https://github.com/wangshijun/course-javascript-async-await/blob/master/07-use-await-with-any-thenable.js)\n1. [在 for 循环中正确的使用 await](https://github.com/wangshijun/course-javascript-async-await/blob/master/08-await-in-loops.js)\n\n## 运行环境\n\n* Node.js v8.6.0\n* VSCode v1.17.1 + Vim\n\n## 参考资料\n\n* [Google Web Fundamentals](https://developers.google.com/web/fundamentals/getting-started/primers/async-functions)\n\n## 视频下载\n\n关注《前端周刊》微信公众号，回复 course-async-await 即可获取高清视频教程下载地址。\n\n![README.png](./README.png)\n\n## 题外话\n\n最后，可能你有同学会问，我为什么要做这个公开课？接下来的计划是什么？请阅读[好久不见，我总感觉欠你点什么](https://mp.weixin.qq.com/s/sH3jpHb7ry2_fF3OVrQ72Q)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwangshijun%2Fcourse-javascript-async-await","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwangshijun%2Fcourse-javascript-async-await","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwangshijun%2Fcourse-javascript-async-await/lists"}