Public
Edited
Jun 1, 2023
Paused
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
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;
}
}
}
Insert cell
IsPromise(Promise.resolve(42))
Insert cell
IsPromise(new Promise(resolve => resolve(42)))
Insert cell
{
class MyPromise extends Promise {
constructor(executor) {
super(executor);
}
}
return IsPromise(new MyPromise(resolve => resolve(42)));
}
Insert cell
Insert cell
function CanPassthroughPromiseResolve(x) {
if (IsPromise(x)) {
const xConstructor = x.constructor;
if (xConstructor === Promise) {
return true;
}
}
return false;
}
Insert cell
CanPassthroughPromiseResolve(Promise.resolve(42))
Insert cell
CanPassthroughPromiseResolve(new Promise(resolve => resolve(42)))
Insert cell
{
class MyPromise extends Promise {
constructor(executor) {
super(executor);
}
}
return CanPassthroughPromiseResolve(new MyPromise(resolve => resolve(42)));
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more