Skip to content

Commit

Permalink
fix(promises): throw TypeError from promisified function when no Promise
Browse files Browse the repository at this point in the history
until now it throws less meaningful error such as "boolean is not a function" - which not make sense
to the end user

fixes #16
  • Loading branch information
tunnckoCore committed Mar 13, 2017
1 parent de17b5f commit c88bac1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
14 changes: 11 additions & 3 deletions dist/redolent.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,10 @@ var index = function redolent (fn, opts) {
opts.context = this || opts.context;
opts.args = index$2(opts.args).concat(index$4(arguments));

if (typeof opts.Promise !== 'function') {
throw new TypeError('redolent: no native Promise support and no opts.Promise')
}

var promise = new opts.Promise(function (resolve, reject) {
var called = false;

Expand All @@ -376,10 +380,14 @@ var index = function redolent (fn, opts) {
}
});

promise.___nativePromise = Boolean(index$10);
promise.___customPromise = !promise.___nativePromise;
return promise
return normalize(promise, index$10)
}
};

function normalize (promise, isNativeSupported) {
promise.___nativePromise = Boolean(isNativeSupported);
promise.___customPromise = !promise.___nativePromise;
return promise
}

module.exports = index;
14 changes: 11 additions & 3 deletions dist/redolent.es.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,10 @@ var index = function redolent (fn, opts) {
opts.context = this || opts.context;
opts.args = index$2(opts.args).concat(index$4(arguments));

if (typeof opts.Promise !== 'function') {
throw new TypeError('redolent: no native Promise support and no opts.Promise')
}

var promise = new opts.Promise(function (resolve, reject) {
var called = false;

Expand All @@ -374,10 +378,14 @@ var index = function redolent (fn, opts) {
}
});

promise.___nativePromise = Boolean(index$10);
promise.___customPromise = !promise.___nativePromise;
return promise
return normalize(promise, index$10)
}
};

function normalize (promise, isNativeSupported) {
promise.___nativePromise = Boolean(isNativeSupported);
promise.___customPromise = !promise.___nativePromise;
return promise
}

export default index;
14 changes: 11 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ module.exports = function redolent (fn, opts) {
opts.context = this || opts.context
opts.args = arrify(opts.args).concat(sliced(arguments))

if (typeof opts.Promise !== 'function') {
throw new TypeError('redolent: no native Promise support and no opts.Promise')
}

var promise = new opts.Promise(function (resolve, reject) {
var called = false

Expand All @@ -106,8 +110,12 @@ module.exports = function redolent (fn, opts) {
}
})

promise.___nativePromise = Boolean(Native)
promise.___customPromise = !promise.___nativePromise
return promise
return normalize(promise, Native)
}
}

function normalize (promise, isNativeSupported) {
promise.___nativePromise = Boolean(isNativeSupported)
promise.___customPromise = !promise.___nativePromise
return promise
}
16 changes: 16 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,30 @@ function factory (promisify) {
})
}

function factoryNoPromise (fn, opts) {
test('should throw TypeError from promisified function if no Promise', function (done) {
function fixture () {
fn(function () {
return 123
}, opts)()
}

test.throws(fixture, TypeError)
test.throws(fixture, /no native Promise support and no opts\.Promise/)
done()
})
}

if (semver.lt(process.version, '0.11.13')) {
factory(function (fn, opts) {
return redolent(fn, extend({
Promise: Pinkie
}, opts))
})
factoryNoPromise(redolent)
} else {
factory(function (fn, opts) {
return redolent(fn, extend({}, opts))
})
factoryNoPromise(redolent, { Promise: false })
}

0 comments on commit c88bac1

Please sign in to comment.