-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
docs: remove async-function #1713
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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
This file was deleted.
Oops, something went wrong.
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
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
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
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 |
---|---|---|
|
@@ -25,7 +25,8 @@ Egg 的理念之一是`渐进式增强`,故我们为开发者提供`渐进升 | |
## 快速升级 | ||
|
||
- Node.js 使用最新的 LTS 版本(`>=8.9.0`)。 | ||
- 执行 `egg-bin autod`,会自动升级 `egg` 的依赖为 `^2.0.0`,以及其他插件版本。 | ||
- 修改 `package.json` 中 `egg` 的依赖为 `^2.0.0`。 | ||
- 检查相关插件是否发布新版本(可选)。 | ||
- 重新安装依赖,跑单元测试。 | ||
|
||
**搞定!不需要修改任何一行代码,就已经完成了升级。** | ||
|
@@ -44,7 +45,9 @@ Egg 的理念之一是`渐进式增强`,故我们为开发者提供`渐进升 | |
|
||
> 2.x 仍然保持对 1.x 风格的中间件的兼容,故不修改也能继续使用。 | ||
|
||
- 返回的函数入参改为 `(ctx, next)` 的形式。 | ||
- 返回的函数入参改为 Koa 2 的 `(ctx, next)` 风格。 | ||
- 第一个参数为 `ctx`,代表当前请求的上下文,是 [Context](./basics/extend.md#Context) 的实例。 | ||
- 第二个参数为 `next`,用 await 执行它来执行后续中间件的逻辑。 | ||
- 不建议使用 `async (ctx, next) => {}` 格式,避免错误堆栈丢失函数名。 | ||
- `yield next` 改为函数调用 `await next()` 的方式。 | ||
|
||
|
@@ -63,7 +66,7 @@ module.exports = () => { | |
module.exports = () => { | ||
return async function responseTime(ctx, next) { | ||
const start = Date.now(); | ||
// 注意:函数调用 | ||
// 注意,和 generator function 格式的中间件不同,此时 next 是一个方法,必须要调用它 | ||
await next(); | ||
const delta = Math.ceil(Date.now() - start); | ||
ctx.set('X-Response-Time', delta + 'ms'); | ||
|
@@ -84,7 +87,7 @@ module.exports = () => { | |
- generators (delegation) | ||
- generator functions (delegation) | ||
|
||
而原生的 `yield` 和 `await` 只支持其中的一部分,故在移除 `co` 后,我们需要根据不同场景自行处理: | ||
尽管 `generator` 和 `async` 两者的编程模型基本一模一样,但由于上述的 `co` 的一些特殊处理,导致在移除 `co` 后,我们需要根据不同场景自行处理: | ||
|
||
#### promise | ||
|
||
|
@@ -177,10 +180,11 @@ const { news, user } = await app.toPromise(ctx.service.biz.list(topic, uid)); | |
- [toAsyncFunction][app.toAsyncFunction] 和 [toPromise][app.toPromise] 实际使用的是 [co] 包装,因此会带回对应的性能损失和堆栈问题,建议开发者还是尽量全链路升级。 | ||
- [toAsyncFunction][app.toAsyncFunction] 在调用 async function 时不会有损失。 | ||
|
||
@sindresorhus 编写了许多[基于 promise 的 helper 方法](https://github.com/sindresorhus/promise-fun),灵活的运用它们配合 async function 能让代码更加具有可读性。 | ||
|
||
## 插件升级 | ||
|
||
`应用开发者`只需升级`插件开发者`修改后的依赖版本即可,可以用我们提供的命令 `egg-bin autod` 快速更新。 | ||
`应用开发者`只需升级`插件开发者`修改后的依赖版本即可,也可以用我们提供的命令 `egg-bin autod` 快速更新。 | ||
|
||
以下内容针对`插件开发者`,指导如何升级插件: | ||
|
||
|
@@ -231,6 +235,12 @@ task = app.toAsyncFunction(schedule.task); | |
- 修改 `README.md` 的示例为 async function。 | ||
- 修改 `test/fixtures` 为 async function,可选,建议分开另一个 PR 方便 Review。 | ||
|
||
一般还会需要继续维护上一个版本,故需要: | ||
- 对上一个版本建立一个 `1.x` 这类的 branch 分支 | ||
- 修改上一个版本的 `package.json` 的 `publishConfig.tag` 为 `release-1.x` | ||
- 这样如果上一个版本有 BugFix 时,npm 版本时就会发布为 `release-1.x` 这个 tag,用户通过 `npm i [email protected]` 来引入旧版本。 | ||
- 参见 [npm 文档](https://docs.npmjs.com/cli/dist-tag)。 | ||
|
||
|
||
[co]: https://github.com/tj/co | ||
[egg-schedule]: https://github.com/eggjs/egg-schedule | ||
|
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.
async 那篇一些内容移到这边来了