Skip to content
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

feat: suport number string & other kinds of iterables #332

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Readme.md
Expand Up @@ -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]
Expand Down Expand Up @@ -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!
Expand Down
7 changes: 4 additions & 3 deletions index.js
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -196,7 +197,7 @@ function objectToPromise(obj){
*/

function isPromise(obj) {
return 'function' == typeof obj.then;
return obj && 'function' == typeof obj.then;
}

/**
Expand Down
6 changes: 3 additions & 3 deletions test/invalid.js
Expand Up @@ -3,15 +3,15 @@ var assert = require('assert');

var co = require('..');

describe('yield <invalid>', function () {
describe('yield* <invalid>', 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('is not a function'));
}
})
})
Expand Down
25 changes: 0 additions & 25 deletions test/thunks.js
Expand Up @@ -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 = [];
Expand Down