promiseOrCallback.js
1.25 KB
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
'use strict';
const PromiseProvider = require('../promise_provider');
const immediate = require('./immediate');
const emittedSymbol = Symbol('mongoose:emitted');
module.exports = function promiseOrCallback(callback, fn, ee, Promise) {
if (typeof callback === 'function') {
return fn(function(error) {
if (error != null) {
if (ee != null && ee.listeners != null && ee.listeners('error').length > 0 && !error[emittedSymbol]) {
error[emittedSymbol] = true;
ee.emit('error', error);
}
try {
callback(error);
} catch (error) {
return immediate(() => {
throw error;
});
}
return;
}
callback.apply(this, arguments);
});
}
Promise = Promise || PromiseProvider.get();
return new Promise((resolve, reject) => {
fn(function(error, res) {
if (error != null) {
if (ee != null && ee.listeners != null && ee.listeners('error').length > 0 && !error[emittedSymbol]) {
error[emittedSymbol] = true;
ee.emit('error', error);
}
return reject(error);
}
if (arguments.length > 2) {
return resolve(Array.prototype.slice.call(arguments, 1));
}
resolve(res);
});
});
};