Last active
July 5, 2017 00:30
-
-
Save devsnek/15f2c9675c3faa9df0d63223e283ad35 to your computer and use it in GitHub Desktop.
promise_util
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "promise_util", | |
"version": "1.3.0", | |
"main": "promise_util.js" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { createPromise, promiseResolve, promiseReject } = process.binding('util'); | |
const { resolve: originalResolve, reject: originalReject } = Promise; | |
const Debug = require('vm').runInDebugContext('Debug'); | |
(function() { | |
if (Promise.create) return; | |
Promise.create = createPromise; | |
Promise.resolve = function(...args) { | |
if (args.length === 2 && args[0] instanceof Promise) { | |
return promiseResolve(args[0], args[1]); | |
} | |
return originalResolve.apply(this, args); | |
}; | |
Promise.reject = function(...args) { | |
if (arguments.length === 2 && args[0] instanceof Promise) { | |
return promiseReject(args[0], args[1]); | |
} | |
return originalReject.apply(this, args); | |
}; | |
Object.defineProperties(Promise.prototype, { | |
info: { get() { return getPromiseInfo(this) } }, | |
isResolved: { get() { return this.info.status === 'resolved'; } }, | |
isRejected: { get() { return this.info.status === 'rejected'; } }, | |
isPending: { get() { return this.info.status === 'pending'; } }, | |
}); | |
Promise.prototype.resolve = function(x) { | |
promiseResolve(this, x); | |
}; | |
Promise.prototype.reject = function(x) { | |
promiseReject(this, x); | |
}; | |
Promise.delay = function(time) { | |
const p = Promise.create(); | |
setTimeout(p.resolve.bind(p), time); | |
return p; | |
}; | |
}()); | |
function getPromiseInfo(promise) { | |
let mirror = Debug.MakeMirror(promise, true); | |
if (!mirror.isPromise()) return { status: 'resolved', value: arg }; | |
let status = mirror.status(); | |
if (status === 'pending') return { status }; | |
let value = mirror.promiseValue().value(); | |
return { status, value }; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment