function IsPromise(x) {
const hasConstructor = Object.prototype.hasOwnProperty.call(x, 'constructor');
const savedConstructor = x.constructor;
x.constructor = undefined;
try {
Promise.prototype.then.call(x, () => {});
return true;
} catch(e) {
return false;
} finally {
if (hasConstructor) {
x.constructor = savedConstructor;
} else {
delete x.constructor;
}
}
}