Skip to content

Commit

Permalink
fixes #180 (memory leak)
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh authored and jonathanong committed Dec 26, 2014
1 parent 987d642 commit efbcf13
Showing 1 changed file with 48 additions and 46 deletions.
94 changes: 48 additions & 46 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,56 +41,58 @@ co.wrap = function (fn) {
function co(gen) {
var ctx = this;
if (typeof gen === 'function') gen = gen.call(this);
return Promise.resolve(onFulfilled());

/**
* @param {Mixed} res
* @return {Promise}
* @api private
*/

function onFulfilled(res) {
var ret;
try {
ret = gen.next(res);
} catch (e) {
return Promise.reject(e);
return new Promise(function(resolve, reject) {
onFulfilled();

/**
* @param {Mixed} res
* @return {Promise}
* @api private
*/

function onFulfilled(res) {
var ret;
try {
ret = gen.next(res);
} catch (e) {
return reject(e);
}
next(ret);
}
return next(ret);
}

/**
* @param {Error} err
* @return {Promise}
* @api private
*/

function onRejected(err) {
var ret;
try {
ret = gen.throw(err);
} catch (e) {
return Promise.reject(e);
/**
* @param {Error} err
* @return {Promise}
* @api private
*/

function onRejected(err) {
var ret;
try {
ret = gen.throw(err);
} catch (e) {
return reject(e);
}
next(ret);
}
return next(ret);
}

/**
* Get the next value in the generator,
* return a promise.
*
* @param {Object} ret
* @return {Promise}
* @api private
*/

function next(ret) {
if (ret.done) return Promise.resolve(ret.value);
var value = toPromise.call(ctx, ret.value);
if (value && isPromise(value)) return value.then(onFulfilled, onRejected);
return onRejected(new TypeError('You may only yield a function, promise, generator, array, or object, '
+ 'but the following object was passed: "' + String(ret.value) + '"'));
}
/**
* Get the next value in the generator,
* return a promise.
*
* @param {Object} ret
* @return {Promise}
* @api private
*/

function next(ret) {
if (ret.done) return resolve(ret.value);
var value = toPromise.call(ctx, ret.value);
if (value && isPromise(value)) return value.then(onFulfilled, onRejected);
return onRejected(new TypeError('You may only yield a function, promise, generator, array, or object, '
+ 'but the following object was passed: "' + String(ret.value) + '"'));
}
});
}

/**
Expand Down

0 comments on commit efbcf13

Please sign in to comment.