Skip to content

Commit

Permalink
fix: dump async function as AsyncFunction (eggjs#1687)
Browse files Browse the repository at this point in the history
  • Loading branch information
dead-horse authored Nov 20, 2017
1 parent 12edd64 commit 757f275
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 6 deletions.
6 changes: 3 additions & 3 deletions app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,12 +182,12 @@ const proto = module.exports = {
},

/**
* Run generator function in the background
* @param {Generator} scope - generator function, the first args is ctx
* Run async function in the background
* @param {Function} scope - the first args is ctx
* ```js
* this.body = 'hi';
*
* this.runInBackground(function* saveUserInfo(ctx) {
* this.runInBackground(async ctx => {
* yield ctx.mysql.query(sql);
* yield ctx.curl(url);
* });
Expand Down
4 changes: 2 additions & 2 deletions lib/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ class Application extends EggApplication {
}

/**
* Run generator function in the background
* Run async function in the background
* @see Context#runInBackground
* @param {Generator} scope - generator function, the first args is an anonymous ctx
* @param {Function} scope - the first args is an anonymous ctx
*/
runInBackground(scope) {
const ctx = this.createAnonymousContext();
Expand Down
4 changes: 3 additions & 1 deletion lib/core/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ function convertValue(key, value, ignore) {

// support generator function
if (is.function(value)) {
return is.generatorFunction(value) ? `<GeneratorFunction ${name}>` : `<Function ${name}>`;
if (is.generatorFunction(value)) return `<GeneratorFunction ${name}>`;
if (is.asyncFunction(value)) return `<AsyncFunction ${name}>`;
return `<Function ${name}>`;
}

const typeName = value.constructor.name;
Expand Down
2 changes: 2 additions & 0 deletions test/lib/core/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ describe('test/lib/core/utils.test.js', () => {
/* eslint object-shorthand: 0 */
anonymousFunction$: function(a) { console.log(a); },
generatorFunction$: function* a(a) { console.log(a); },
asyncFunction$: async function a(a) { console.log(a); },
};
utils.convertObject(obj);
assert(obj.function$ === '<Function a>');
assert(obj.arrowFunction$ === '<Function arrowFunction$>');
assert(obj.anonymousFunction$ === '<Function anonymousFunction$>');
assert(obj.generatorFunction$ === '<GeneratorFunction a>');
assert(obj.asyncFunction$ === '<AsyncFunction a>');
});

it('should convert error', () => {
Expand Down

0 comments on commit 757f275

Please sign in to comment.