Comparing version 4.0.5 to 4.1.0
39
index.js
@@ -5,2 +5,20 @@ var EventEmitter = require('events').EventEmitter | ||
class WattPromise { | ||
constructor () { | ||
// hack to check if the consumer is checking for rejections | ||
this.handlingReject = false | ||
this.promise = new Promise(...arguments) | ||
} | ||
then (onFulfilled, onRejected) { | ||
if (onRejected) this.handlingReject = true | ||
this.promise.then(onFulfilled, onRejected) | ||
} | ||
catch (onRejected) { | ||
if (onRejected) this.handlingReject = true | ||
this.promise.catch(onRejected) | ||
} | ||
} | ||
function Watt (gen, args, opts, cb) { | ||
@@ -87,2 +105,3 @@ if (typeof args === 'function') { | ||
module.exports.Watt = Watt | ||
module.exports.WattPromise = WattPromise | ||
@@ -101,6 +120,6 @@ Watt.prototype._callCb = function () { | ||
// if no cb is specified, return a Promise instead | ||
this._promise = new Promise((resolve, reject) => { | ||
this._promise = new WattPromise((resolve, reject) => { | ||
this._cb = (err, res) => { | ||
if (err) { | ||
if (!handlingReject) { | ||
if (!this._promise.handlingReject) { | ||
if (process.listeners && process.listeners('uncaughtException').length) { | ||
@@ -120,14 +139,2 @@ return process.emit('uncaughtException', err) | ||
}) | ||
// hack to check if the consumer is checking for rejections | ||
var handlingReject = false | ||
var then = this._promise.then.bind(this._promise) | ||
var _catch = this._promise.catch.bind(this._promise) | ||
this._promise.then = (onFulfilled, onRejected) => { | ||
if (onRejected) handlingReject = true | ||
then(onFulfilled, onRejected) | ||
} | ||
this._promise.catch = (onRejected) => { | ||
if (onRejected) handlingReject = true | ||
_catch(onRejected) | ||
} | ||
return this._promise | ||
@@ -154,3 +161,3 @@ } | ||
} | ||
if (res.value instanceof Promise) { | ||
if (res.value instanceof WattPromise) { | ||
res.value.then(this.next.bind(this), this.error.bind(this)) | ||
@@ -207,3 +214,3 @@ } | ||
var syncGroup = this._syncGroup | ||
return new Promise((resolve, reject) => { | ||
return new WattPromise((resolve, reject) => { | ||
if (!syncGroup) return resolve(null) | ||
@@ -210,0 +217,0 @@ function onFinish (resolve, reject) { |
{ | ||
"name": "gigawatts", | ||
"version": "4.0.5", | ||
"version": "4.1.0", | ||
"description": "Powerful generator control flow (fork of watt)", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
var test = require('tap').test | ||
var watt = require('.') | ||
var WattPromise = watt.WattPromise | ||
@@ -71,3 +72,3 @@ test('simple wrap', (t) => { | ||
})() | ||
t.ok(promise instanceof Promise, 'watt function returned Promise') | ||
t.ok(promise instanceof WattPromise, 'watt function returned Promise') | ||
promise.then( | ||
@@ -161,3 +162,3 @@ (res) => { | ||
}, { noCallback: true })(() => t.fail('callback should not have been called')) | ||
t.ok(res instanceof Promise, 'watt function returned promise') | ||
t.ok(res instanceof WattPromise, 'watt function returned promise') | ||
}) | ||
@@ -164,0 +165,0 @@ |
31095
660