Skip to content

Commit ae38fa4

Browse files
Maledongdead-horse
authored andcommitted
chroe: add more comments for toAsyncFunction and toPromise
1 parent 4d4113c commit ae38fa4

3 files changed

Lines changed: 34 additions & 8 deletions

File tree

lib/egg.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,9 +351,20 @@ class EggCore extends KoaApplication {
351351
}
352352

353353
/**
354-
* use co to wrap generator function to a function return promise
355-
* @param {GeneratorFunction} fn input function
356-
* @return {AsyncFunction} async function return promise
354+
* Convert a generator function to a promisable one.
355+
*
356+
* Notice: for other kinds of functions, it directly returns you what it is.
357+
*
358+
* @param {Function} fn The inputted function.
359+
* @return {AsyncFunction} An async promise-based function.
360+
* @example
361+
* ```javascript
362+
* const fn = function* (arg) {
363+
return arg;
364+
};
365+
const wrapped = app.toAsyncFunction(fn);
366+
wrapped(true).then((value) => console.log(value));
367+
* ```
357368
*/
358369
toAsyncFunction(fn) {
359370
if (!is.generatorFunction(fn)) return fn;
@@ -364,9 +375,18 @@ class EggCore extends KoaApplication {
364375
}
365376

366377
/**
367-
* use co to wrap array or object to a promise
368-
* @param {Mixed} obj input object
369-
* @return {Promise} promise
378+
* Convert an object with generator functions to a Promisable one.
379+
* @param {Mixed} obj The inputted object.
380+
* @return {Promise} A Promisable result.
381+
* @example
382+
* ```javascript
383+
* const fn = function* (arg) {
384+
return arg;
385+
};
386+
const arr = [ fn(1), fn(2) ];
387+
const promise = app.toPromise(arr);
388+
promise.then(res => console.log(res));
389+
* ```
370390
*/
371391
toPromise(obj) {
372392
return co(function* () {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
"types": "index.d.ts",
77
"scripts": {
88
"autod": "autod",
9-
"lint": "eslint lib test *.js",
10-
"test": "npm run lint && egg-bin test",
9+
"lint": "eslint .",
10+
"test": "npm run lint -- --fix && egg-bin test",
1111
"test-local": "egg-bin test",
1212
"cov": "egg-bin cov",
1313
"pkgfiles": "egg-bin pkgfiles",

test/egg.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,12 @@ describe('test/egg.test.js', () => {
415415
const wrapped = app.toAsyncFunction(fn);
416416
return wrapped(true).then(res => assert(res === true));
417417
});
418+
419+
it('not translate common values', () => {
420+
const primitiveValues = [ 1, 2, 3, 4, 5, 6 ];
421+
const wrapped = app.toAsyncFunction(primitiveValues);
422+
return assert(wrapped === primitiveValues);
423+
});
418424
});
419425

420426
describe('toPromise', () => {

0 commit comments

Comments
 (0)