- passes (null, value) to callback on resolution
const value = {};
const promise = PPromise.resolve(value);
promiseNodeify(promise, function(err, result) {
assert.strictEqual(arguments.length, 2);
assert.strictEqual(err, null);
assert.strictEqual(result, value);
done();
});
- passes undefined value to callback on resolution
let value;
const promise = PPromise.resolve(value);
promiseNodeify(promise, (err, result) => {
// Note: arguments.length is unspecified
// Although it is currently 2, that may change. Don't depend on it.
assert.strictEqual(err, null);
assert.strictEqual(result, value);
done();
});
- passes Error cause to callback on rejection
const cause = new Error();
const promise = PPromise.reject(cause);
promiseNodeify(promise, function(err) {
assert.strictEqual(arguments.length, 1);
assert.strictEqual(err, cause);
done();
});
- passes truthy cause to callback on rejection
const cause = true;
const promise = PPromise.reject(cause);
promiseNodeify(promise, function(err) {
assert.strictEqual(arguments.length, 1);
assert.strictEqual(err, cause);
done();
});
- passes Error with falsey .cause on rejection
const cause = 0;
const promise = PPromise.reject(cause);
promiseNodeify(promise, function(err) {
assert.strictEqual(arguments.length, 1);
assert(err instanceof Error);
assert.strictEqual(err.message, String(cause));
assert(hasOwnProperty.call(err, 'cause'));
assert.strictEqual(err.cause, cause);
done();
});
- returns undefined
const value = {};
const promise = PPromise.resolve(value);
const retVal = promiseNodeify(promise, () => {});
assert.strictEqual(retVal, undefined);
- ignores callback return value
const value = {};
const promise = PPromise.resolve(value);
promiseNodeify(promise, () => true);
return promise.then((result) => {
assert.strictEqual(result, value);
});
- callback exception causes uncaughtException
const value = {};
const errCallback = new Error('Test callback error');
const promise = PPromise.resolve(value);
let unhandledRejection;
function onUnhandledRejection(reason) {
unhandledRejection = reason;
}
process.once('unhandledRejection', onUnhandledRejection);
promiseNodeify(promise, () => { throw errCallback; });
return promiseFinally(
awaitGlobalException((err) => {
assert.strictEqual(err, errCallback);
}),
() => {
process.removeListener('unhandledRejection', onUnhandledRejection);
return unhandledRejection && PPromise.reject(unhandledRejection);
}
);