mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-22 09:23:21 +01:00
Fix nodeify
This commit is contained in:
parent
80e76b45ee
commit
9e764b1935
@ -1,59 +1,17 @@
|
||||
/* NODEIFY
|
||||
* Modified from original npm package "nodeify"
|
||||
* https://github.com/then/nodeify
|
||||
*
|
||||
* Removed Promise dependency, to only support
|
||||
* native Promises and reduce bundle size.
|
||||
*/
|
||||
|
||||
var isPromise = require('is-promise')
|
||||
|
||||
var nextTick
|
||||
if (typeof setImmediate === 'function') nextTick = setImmediate
|
||||
else if (typeof process === 'object' && process && process.nextTick) nextTick = process.nextTick
|
||||
else nextTick = function (cb) { setTimeout(cb, 0) }
|
||||
|
||||
module.exports = nodeify
|
||||
function nodeify(promise, cb) {
|
||||
if (typeof cb !== 'function') return promise
|
||||
return promise
|
||||
.then(function (res) {
|
||||
nextTick(function () {
|
||||
cb(null, res)
|
||||
})
|
||||
}, function (err) {
|
||||
nextTick(function () {
|
||||
cb(err)
|
||||
})
|
||||
})
|
||||
}
|
||||
function nodeifyThis(cb) {
|
||||
return nodeify(this, cb)
|
||||
}
|
||||
|
||||
nodeify.extend = extend
|
||||
nodeify.Promise = NodeifyPromise
|
||||
|
||||
function extend(prom) {
|
||||
if (prom && isPromise(prom)) {
|
||||
prom.nodeify = nodeifyThis
|
||||
var then = prom.then
|
||||
prom.then = function () {
|
||||
return extend(then.apply(this, arguments))
|
||||
module.exports = function (promiseFn) {
|
||||
return function () {
|
||||
var args = []
|
||||
for (var i = 0; i < arguments.length - 1; i++) {
|
||||
args.push(arguments[i])
|
||||
}
|
||||
return prom
|
||||
} else if (typeof prom === 'function') {
|
||||
prom.prototype.nodeify = nodeifyThis
|
||||
var cb = arguments[arguments.length - 1]
|
||||
|
||||
return promiseFn.apply(this, args)
|
||||
.then(function (result) {
|
||||
cb(null, result)
|
||||
})
|
||||
.catch(function (reason) {
|
||||
cb(reason)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
function NodeifyPromise(fn) {
|
||||
if (!(this instanceof NodeifyPromise)) {
|
||||
return new NodeifyPromise(fn)
|
||||
}
|
||||
Promise.call(this, fn)
|
||||
extend(this)
|
||||
}
|
||||
|
||||
NodeifyPromise.prototype = Object.create(Promise.prototype)
|
||||
NodeifyPromise.prototype.constructor = NodeifyPromise
|
||||
|
22
test/unit/nodeify-test.js
Normal file
22
test/unit/nodeify-test.js
Normal file
@ -0,0 +1,22 @@
|
||||
const assert = require('assert')
|
||||
const nodeify = require('../../app/scripts/lib/nodeify')
|
||||
|
||||
describe.only('nodeify', function() {
|
||||
|
||||
var obj = {
|
||||
foo: 'bar',
|
||||
promiseFunc: function (a) {
|
||||
var solution = this.foo + a
|
||||
return Promise.resolve(solution)
|
||||
}
|
||||
}
|
||||
|
||||
it('should retain original context', function(done) {
|
||||
var nodified = nodeify(obj.promiseFunc).bind(obj)
|
||||
nodified('baz', function (err, res) {
|
||||
assert.equal(res, 'barbaz')
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
})
|
Loading…
Reference in New Issue
Block a user