blob: f48df34ef900852cb2c79117be97f0c7d2193746 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
/* 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))
}
return prom
} else if (typeof prom === 'function') {
prom.prototype.nodeify = nodeifyThis
}
}
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
|