Skip to content

Instantly share code, notes, and snippets.

@matthewhively
Created July 12, 2022 00:29
Show Gist options
  • Save matthewhively/59b36c874df3576983a7621cdebba48d to your computer and use it in GitHub Desktop.
Save matthewhively/59b36c874df3576983a7621cdebba48d to your computer and use it in GitHub Desktop.
Test if can function as a promise. Copied from paypal sdk/js for subscriptions
function is_promise_like(n) {
try {
if (!n)
{cl('falsy'); return !1; }
if ("undefined" != typeof Promise && n instanceof Promise)
{cl('is Promise (ok)'); return !0; }
if ("undefined" != typeof window && "function" == typeof window.Window && n instanceof window.Window)
{cl('is Window'); return !1; }
if ("undefined" != typeof window && "function" == typeof window.constructor && n instanceof window.constructor)
{cl('is constructor'); return !1; }
var e = {}.toString;
if (e) {
var t = e.call(n);
if ("[object Window]" === t || "[object global]" === t || "[object DOMWindow]" === t)
{cl('obj type'); return !1 }
}
if ("function" == typeof n.then)
{cl('has then (ok)'); return !0 }
} catch (n) {
cl('exception');
return !1
}
cl('other');
return !1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment