Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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