Skip to content

Instantly share code, notes, and snippets.

@akhoury
Last active April 10, 2019 14:31
Show Gist options
  • Save akhoury/622acbe3bb4c29412657e964ac9af7ea to your computer and use it in GitHub Desktop.
Save akhoury/622acbe3bb4c29412657e964ac9af7ea to your computer and use it in GitHub Desktop.
How to test if an object is promise in the real world
// you must install the 3 modules for this to work
// npm install bluebird q when
// so the gist here, no pun intended, is that when you're testing for a Promise, it's totally fine
// to either check if the object is instanceof Promise or even if Promise.resolve(object) == object
// UNLESS there is a chance that this promise is returned from a third party promise library, or even
// if you have plugins or some other modules using your module,
// ... so, if you don't know which "Promise" constructor could be getting,
// you should use the "wrong way" which is basically checking if tyepof object.then === "function"
// I do understand that { then: () => {} } will wrongly pass the test, but if you are returning object with a then() function
// then you're an as*hole, and if any other module is also doing, then that author is an as*hole
const NativePromiseConstructor = global.Promise;
const BluebirdPromiseConstructor = require('bluebird').Promise;
const QPromiseConstructor = require('q').Promise;
const WhenPromiseConstructor = require('when').Promise;
const promiseTypes = [
{
PromiseConstructor: NativePromiseConstructor,
name: 'Native'
},
{
PromiseConstructor: BluebirdPromiseConstructor,
name: 'Bluebird'
},
{
PromiseConstructor: QPromiseConstructor,
name: 'Q'
},
{
PromiseConstructor: WhenPromiseConstructor,
name: 'When'
}
];
const isPromiseWithResolve = function (object, PromiseConstructor) {
if (!PromiseConstructor) {
PromiseConstructor = NativePromiseConstructor;
}
return object && PromiseConstructor.resolve(object) === object;
};
const isPromiseWithInstanceOf = function (object, PromiseConstructor) {
if (!PromiseConstructor) {
PromiseConstructor = NativePromiseConstructor;
}
return object && object instanceof PromiseConstructor;
};
const isPromiseWithTypeofThen = function (object) {
return object && typeof object.then === 'function';
};
console.log(`\nisPromiseWithResolve\n`);
promiseTypes.forEach(function(type) {
let ret = isPromiseWithResolve(new type.PromiseConstructor(() => {}));
console.log(`Testing "${type.name}", isPromiseWithResolve() returned\n${ret}, ${ret ? 'Good!' : 'Not good, should\'ve been true'}`);
});
console.log(`\nisPromiseWithResolve while using the corresponding PromiseConstructor\n`);
promiseTypes.forEach(function(type) {
let ret = isPromiseWithResolve(new type.PromiseConstructor(() => {}), type.PromiseConstructor);
console.log(`Testing "${type.name}", isPromiseWithResolve() returned\n${ret}, ${ret ? 'Good!' : 'Not good, should\'ve been true'}`);
});
console.log(`\nisPromiseWithInstanceOf\n`);
promiseTypes.forEach(function(type) {
let ret = isPromiseWithInstanceOf(new type.PromiseConstructor(() => {}));
console.log(`Testing "${type.name}", isPromiseWithInstanceOf() returned\n${ret}, ${ret ? 'Good!' : 'Not good, should\'ve been true'}`);
});
console.log(`\nisPromiseWithInstanceOf while using the corresponding PromiseConstructor\n`);
promiseTypes.forEach(function(type) {
let ret = isPromiseWithInstanceOf(new type.PromiseConstructor(() => {}), type.PromiseConstructor);
console.log(`Testing "${type.name}", isPromiseWithInstanceOf() returned\n${ret}, ${ret ? 'Good!' : 'Not good, should\'ve been true'}`);
});
console.log(`\nisPromiseWithTypeofThen\n`);
promiseTypes.forEach(function(type) {
let ret = isPromiseWithTypeofThen(new type.PromiseConstructor(() => {}));
console.log(`Testing "${type.name}", isPromiseWithTypeofThen() returned\n${ret}, ${ret ? 'Good!' : 'Not good, should\'ve been true'}`);
});
/*
====================
=OUTPUT 4 THE LAZYS=
====================
[518] > node check-promise.js
isPromiseWithResolve
Testing "Native", isPromiseWithResolve() returned
true, Good!
Testing "Bluebird", isPromiseWithResolve() returned
false, Not good, should've been true
Testing "Q", isPromiseWithResolve() returned
false, Not good, should've been true
Testing "When", isPromiseWithResolve() returned
false, Not good, should've been true
isPromiseWithResolve while using the corresponding PromiseConstructor
Testing "Native", isPromiseWithResolve() returned
true, Good!
Testing "Bluebird", isPromiseWithResolve() returned
true, Good!
Testing "Q", isPromiseWithResolve() returned
true, Good!
Testing "When", isPromiseWithResolve() returned
true, Good!
isPromiseWithInstanceOf
Testing "Native", isPromiseWithInstanceOf() returned
true, Good!
Testing "Bluebird", isPromiseWithInstanceOf() returned
false, Not good, should've been true
Testing "Q", isPromiseWithInstanceOf() returned
false, Not good, should've been true
Testing "When", isPromiseWithInstanceOf() returned
false, Not good, should've been true
isPromiseWithInstanceOf while using the corresponding PromiseConstructor
Testing "Native", isPromiseWithInstanceOf() returned
true, Good!
Testing "Bluebird", isPromiseWithInstanceOf() returned
true, Good!
Testing "Q", isPromiseWithInstanceOf() returned
false, Not good, should've been true
Testing "When", isPromiseWithInstanceOf() returned
true, Good!
isPromiseWithTypeofThen
Testing "Native", isPromiseWithTypeofThen() returned
true, Good!
Testing "Bluebird", isPromiseWithTypeofThen() returned
true, Good!
Testing "Q", isPromiseWithTypeofThen() returned
true, Good!
Testing "When", isPromiseWithTypeofThen() returned
true, Good!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment