Comparing version 0.6.0 to 0.6.1
@@ -148,11 +148,25 @@ var Promise = require('../node_modules/promise/lib/es6-extensions'); | ||
function Suspender(generator, callback) { | ||
var self = this; | ||
this.generator = generator; | ||
// initialized in start() | ||
this.iterator = null; | ||
// (optionally) initialized in start() | ||
this.callback = callback; | ||
// flag to keep track of whether or not the entire generator completed. | ||
// See start() for state tracking. | ||
this.syncComplete = true; | ||
// makes sure to not unleash zalgo: https://github.com/jmar777/suspend/pull/21 | ||
this.callback = callback && function() { | ||
if (self.syncComplete) { | ||
var args = Array.prototype.slice.call(arguments); | ||
setImmediate(function() { | ||
callback.apply(this, args); | ||
}); | ||
} else { | ||
callback.apply(this, arguments); | ||
} | ||
}; | ||
// flag indicating whether or not the iterator has completed | ||
this.done = false; | ||
// flag to keep track of whether or not we were resumed synchronously. | ||
// See next() for state tracking. | ||
// See nextOrThrow() for state tracking. | ||
this.syncResume = false; | ||
@@ -179,2 +193,3 @@ // flag indicating whether or not the values passed to resume() should be | ||
this.nextOrThrow(); | ||
this.syncComplete = false; | ||
}; | ||
@@ -188,3 +203,5 @@ | ||
this.done = true; | ||
this.callback && this.callback.call(null, null, ret.value); | ||
if (this.callback) { | ||
this.callback.call(null, null, ret.value); | ||
} | ||
return; | ||
@@ -216,2 +233,4 @@ } | ||
Suspender.prototype.nextOrThrow = function next(val, isError) { | ||
var self = this; | ||
this.syncResume = true; | ||
@@ -223,7 +242,11 @@ setActiveSuspender(this); | ||
} catch (err) { | ||
if (this.callback) { | ||
return this.callback(err); | ||
} else { | ||
throw err; | ||
} | ||
// prevents promise swallowing: https://github.com/jmar777/suspend/pull/21 | ||
setImmediate(function() { | ||
if (self.callback) { | ||
return self.callback(err); | ||
} else { | ||
throw err; | ||
} | ||
}); | ||
return; | ||
} finally { | ||
@@ -230,0 +253,0 @@ this.syncResume = false; |
@@ -6,3 +6,3 @@ { | ||
"keywords": ["async", "generator", "yield", "callback", "promise", "flow", "control", "suspend"], | ||
"version": "0.6.0", | ||
"version": "0.6.1", | ||
"repository": { | ||
@@ -9,0 +9,0 @@ "type": "git", |
@@ -365,5 +365,5 @@ # suspend | ||
Additional thanks goes to [Ben Newman](https://github.com/benjamn), [Willem](https://github.com/helmus), [Michael Hart](https://github.com/mhart), and [Sonny Piers](https://github.com/sonnyp) for their contributions to the project. | ||
Additional thanks goes to [Ben Newman](https://github.com/benjamn), [Willem](https://github.com/helmus), [Michael Hart](https://github.com/mhart), [Sonny Piers](https://github.com/sonnyp), and [Yunsong Guo](https://github.com/gyson) for their contributions to the project. | ||
## License | ||
## License | ||
@@ -370,0 +370,0 @@ The MIT License (MIT) |
@@ -126,2 +126,31 @@ var assert = require('assert'), | ||
}); | ||
it('should not unleash zalgo on synchronous completion', function(done) { | ||
var x = 41; | ||
async(function*() { | ||
return; | ||
})(function() { | ||
assert.strictEqual(42, x); | ||
done(); | ||
}); | ||
// this should run before the callback | ||
x += 1; | ||
}); | ||
it('should not unleash zalgo on synchronously thrown errors', function(done) { | ||
var x = 41; | ||
async(function*() { | ||
throw new Error(); | ||
})(function() { | ||
assert.strictEqual(42, x); | ||
done(); | ||
}); | ||
// this should run before the callback | ||
x += 1; | ||
}); | ||
}); | ||
@@ -128,0 +157,0 @@ |
@@ -112,2 +112,30 @@ var assert = require('assert'), | ||
}); | ||
it('should not unleash zalgo on synchronous completion', function(done) { | ||
var x = 41; | ||
suspend.promise(function*() { | ||
return; | ||
})().then(function() { | ||
assert.strictEqual(42, x); | ||
done(); | ||
}); | ||
// this should run before the promise fulfillment | ||
x += 1; | ||
}); | ||
it('should not unleash zalgo on synchronously thrown errors', function(done) { | ||
var x = 41; | ||
suspend.promise(function*() { | ||
throw new Error(); | ||
})().then(noop, function() { | ||
assert.strictEqual(42, x); | ||
done(); | ||
}); | ||
// this should run before the promise fulfillment | ||
x += 1; | ||
}); | ||
}); | ||
@@ -114,0 +142,0 @@ |
@@ -87,2 +87,30 @@ var assert = require('assert'), | ||
}); | ||
it('should not unleash zalgo on synchronous completion', function(done) { | ||
var x = 41; | ||
run(function*() { | ||
return; | ||
}, function() { | ||
assert.strictEqual(42, x); | ||
done(); | ||
}); | ||
// this should run before the callback | ||
x += 1; | ||
}); | ||
it('should not unleash zalgo on synchronously thrown errors', function(done) { | ||
var x = 41; | ||
run(function*() { | ||
throw new Error(); | ||
}, function() { | ||
assert.strictEqual(42, x); | ||
done(); | ||
}); | ||
// this should run before the callback | ||
x += 1; | ||
}); | ||
}); | ||
@@ -89,0 +117,0 @@ |
@@ -24,2 +24,26 @@ var assert = require('assert'), | ||
it('should not swallow exception', function(done) { | ||
captureNextUncaughtException(function(err) { | ||
assert.strictEqual(err.message, 'oops'); | ||
done(); | ||
}); | ||
run(function*() { | ||
yield asyncError(); | ||
}); | ||
}); | ||
it('should not swallow exceptions in callback', function(done) { | ||
captureNextUncaughtException(function(err) { | ||
assert.strictEqual(err.message, 'from callback'); | ||
done(); | ||
}); | ||
run(function*() { | ||
yield asyncError(); | ||
}, function(err) { | ||
if (err) throw new Error('from callback'); | ||
}); | ||
}); | ||
it('should behave correctly when multiple generators run in parallel', function(done) { | ||
@@ -73,1 +97,13 @@ var doneCount = 0, | ||
} | ||
function captureNextUncaughtException(cb) { | ||
var mochaListener = process.listeners('uncaughtException')[0]; | ||
process.removeListener('uncaughtException', mochaListener); | ||
var newListener = function(err) { | ||
// restore mocha's listener | ||
process.removeListener('uncaughtException', newListener); | ||
process.on('uncaughtException', mochaListener); | ||
cb(err); | ||
} | ||
process.on('uncaughtException', newListener); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
53301
1260