From b0d29b077b700fd690aad39784bad0257bd5bbe8 Mon Sep 17 00:00:00 2001 From: CH13G <1120868240@qq.com> Date: Wed, 27 Nov 2019 15:30:18 +0800 Subject: [PATCH 1/2] feat: suport number string & other kinds of iterables --- Readme.md | 4 ++++ index.js | 7 ++++--- test/invalid.js | 6 +++--- test/thunks.js | 25 ------------------------- 4 files changed, 11 insertions(+), 31 deletions(-) diff --git a/Readme.md b/Readme.md index 8ea2bb9..ef9f78f 100644 --- a/Readme.md +++ b/Readme.md @@ -84,6 +84,8 @@ co(function *(){ var a = Promise.resolve(1); var b = Promise.resolve(2); var c = Promise.resolve(3); + yield 1; + yield* '123'; var res = yield [a, b, c]; console.log(res); // => [1, 2, 3] @@ -116,6 +118,8 @@ function onerror(err) { - objects (parallel execution) - generators (delegation) - generator functions (delegation) + - number string undefined (basic type) + - generator other kinds of iterables (delegation) e.g. arrays, strings or arguments objects. Nested `yieldable` objects are supported, meaning you can nest promises within objects within arrays, and so on! diff --git a/index.js b/index.js index aacf6ef..d241bb8 100644 --- a/index.js +++ b/index.js @@ -114,13 +114,14 @@ function co(gen) { */ function toPromise(obj) { - if (!obj) return obj; + if (!obj) return Promise.resolve(obj); if (isPromise(obj)) return obj; if (isGeneratorFunction(obj) || isGenerator(obj)) return co.call(this, obj); if ('function' == typeof obj) return thunkToPromise.call(this, obj); if (Array.isArray(obj)) return arrayToPromise.call(this, obj); if (isObject(obj)) return objectToPromise.call(this, obj); - return obj; + return Promise.resolve(obj) + // return obj; } /** @@ -196,7 +197,7 @@ function objectToPromise(obj){ */ function isPromise(obj) { - return 'function' == typeof obj.then; + return obj && 'function' == typeof obj.then; } /** diff --git a/test/invalid.js b/test/invalid.js index 50c0975..ac0735d 100644 --- a/test/invalid.js +++ b/test/invalid.js @@ -3,15 +3,15 @@ var assert = require('assert'); var co = require('..'); -describe('yield ', function () { +describe('yield* ', function () { it('should throw an error', function () { return co(function* () { try { - yield null; + yield* 123; throw new Error('lol'); } catch (err) { assert(err instanceof TypeError); - assert(~err.message.indexOf('You may only yield')); + assert(~err.message.indexOf('undefined is not a function')); } }) }) diff --git a/test/thunks.js b/test/thunks.js index c7fd1e6..ec10482 100644 --- a/test/thunks.js +++ b/test/thunks.js @@ -182,31 +182,6 @@ describe('co(* -> yield fn(done))', function () { }) }) - describe('when yielding neither a function nor a promise', function(){ - it('should throw', function(){ - var errors = []; - - return co(function *(){ - try { - var a = yield 'something'; - } catch (err) { - errors.push(err.message); - } - - try { - var a = yield 'something'; - } catch (err) { - errors.push(err.message); - } - - assert.equal(2, errors.length); - var msg = 'yield a function, promise, generator, array, or object'; - assert(~errors[0].indexOf(msg)); - assert(~errors[1].indexOf(msg)); - }); - }) - }) - describe('with errors', function(){ it('should throw', function(){ var errors = []; From 156d25cf8c09563c0e2c7006d8932ec8943ed45c Mon Sep 17 00:00:00 2001 From: CH13G <1120868240@qq.com> Date: Wed, 27 Nov 2019 15:52:31 +0800 Subject: [PATCH 2/2] fix: invalid test --- test/invalid.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/invalid.js b/test/invalid.js index ac0735d..4b6a3d2 100644 --- a/test/invalid.js +++ b/test/invalid.js @@ -11,7 +11,7 @@ describe('yield* ', function () { throw new Error('lol'); } catch (err) { assert(err instanceof TypeError); - assert(~err.message.indexOf('undefined is not a function')); + assert(~err.message.indexOf('is not a function')); } }) })