Skip to content

Commit

Permalink
docs(schedule): generator -> async (eggjs#1656)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored and popomore committed Nov 13, 2017
1 parent ac55d5e commit 97c42aa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
22 changes: 11 additions & 11 deletions docs/source/en/basics/schedule.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class UpdateCache extends Subscription {
}

// `subscribe` is the function to be executed when the scheduled task is triggered
* subscribe() {
const res = yield this.ctx.curl('http://www.api.com/cache', {
async subscribe() {
const res = await this.ctx.curl('http://www.api.com/cache', {
dataType: 'json',
});
this.ctx.app.cache = res.data;
Expand All @@ -47,8 +47,8 @@ module.exports = {
interval: '1m', // 1 minute interval
type: 'all', // specify all `workers` need to execute
},
* task(ctx) {
const res = yield ctx.curl('http://www.api.com/cache', {
async task(ctx) {
const res = await ctx.curl('http://www.api.com/cache', {
dataType: 'json',
});
ctx.app.cache = res.data;
Expand Down Expand Up @@ -137,8 +137,8 @@ module.exports = app => {
type: 'all',
disable: app.config.env === 'local', // not execute when local dev
},
* task(ctx) {
const res = yield ctx.curl('http://www.api.com/cache', {
async task(ctx) {
const res = await ctx.curl('http://www.api.com/cache', {
contentType: 'json',
});
ctx.app.cache = res.data;
Expand All @@ -159,10 +159,10 @@ There are some scenarios we may need to manually execute scheduled tasks, for ex
const mm = require('egg-mock');
const assert = require('assert');

it('should schedule work fine', function*() {
it('should schedule work fine', async () => {
const app = mm.app();
yield app.ready();
yield app.runSchedule('update_cache');
await app.ready();
await app.runSchedule('update_cache');
assert(app.cache);
});
```
Expand All @@ -171,10 +171,10 @@ When the application starts up, manually perform the scheduled tasks for system

```js
module.exports = app => {
app.beforeStart(function* () {
app.beforeStart(async () => {
// ensure the data is ready before the application start listening port
// follow-up data updates automatically by the scheduled task
yield app.runSchedule('update_cache');
await app.runSchedule('update_cache');
});
};
```
Expand Down
22 changes: 11 additions & 11 deletions docs/source/zh-cn/basics/schedule.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class UpdateCache extends Subscription {
}

// subscribe 是真正定时任务执行时被运行的函数
* subscribe() {
const res = yield this.ctx.curl('http://www.api.com/cache', {
async subscribe() {
const res = await this.ctx.curl('http://www.api.com/cache', {
dataType: 'json',
});
this.ctx.app.cache = res.data;
Expand All @@ -47,8 +47,8 @@ module.exports = {
interval: '1m', // 1 分钟间隔
type: 'all', // 指定所有的 worker 都需要执行
},
* task(ctx) {
const res = yield ctx.curl('http://www.api.com/cache', {
async task(ctx) {
const res = await ctx.curl('http://www.api.com/cache', {
dataType: 'json',
});
ctx.app.cache = res.data;
Expand Down Expand Up @@ -137,8 +137,8 @@ module.exports = app => {
type: 'all',
disable: app.config.env === 'local', // 本地开发环境不执行
},
* task(ctx) {
const res = yield ctx.curl('http://www.api.com/cache', {
async task(ctx) {
const res = await ctx.curl('http://www.api.com/cache', {
contentType: 'json',
});
ctx.app.cache = res.data;
Expand All @@ -159,10 +159,10 @@ module.exports = app => {
const mm = require('egg-mock');
const assert = require('assert');

it('should schedule work fine', function*() {
it('should schedule work fine', async () => {
const app = mm.app();
yield app.ready();
yield app.runSchedule('update_cache');
await app.ready();
await app.runSchedule('update_cache');
assert(app.cache);
});
```
Expand All @@ -171,10 +171,10 @@ it('should schedule work fine', function*() {

```js
module.exports = app => {
app.beforeStart(function* () {
app.beforeStart(async () => {
// 保证应用启动监听端口前数据已经准备好了
// 后续数据的更新由定时任务自动触发
yield app.runSchedule('update_cache');
await app.runSchedule('update_cache');
});
};
```
Expand Down

0 comments on commit 97c42aa

Please sign in to comment.