Skip to content

Commit

Permalink
Fixes tj#180 (memory leak)
Browse files Browse the repository at this point in the history
Fixes tj#180 by switching to a procedural implementation instead of
building up an entire promise chain of all the values yielded by the
generator.
  • Loading branch information
spion committed Dec 25, 2014
1 parent 987d642 commit c103f82
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,13 @@ co.wrap = function (fn) {
function co(gen) {
var ctx = this;
if (typeof gen === 'function') gen = gen.call(this);
return Promise.resolve(onFulfilled());
var resolve, reject;
var result = new Promise(function(myresolve, myreject) {
resolve = myresolve;
reject = myreject;
});
onFulfilled();
return result;

/**
* @param {Mixed} res
Expand All @@ -54,9 +60,9 @@ function co(gen) {
try {
ret = gen.next(res);
} catch (e) {
return Promise.reject(e);
return reject(e);
}
return next(ret);
next(ret);
}

/**
Expand All @@ -70,9 +76,9 @@ function co(gen) {
try {
ret = gen.throw(err);
} catch (e) {
return Promise.reject(e);
return reject(e);
}
return next(ret);
next(ret);
}

/**
Expand All @@ -85,7 +91,7 @@ function co(gen) {
*/

function next(ret) {
if (ret.done) return Promise.resolve(ret.value);
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, '
Expand Down

0 comments on commit c103f82

Please sign in to comment.