forked from medikoo/deferred
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.js
40 lines (36 loc) · 1.07 KB
/
monitor.js
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
// Run if you want to monitor unresolved promises (in properly working
// application there should be no promises that are never resolved)
'use strict';
var max = Math.max
, callable = require('es5-ext/object/valid-callable')
, isCallable = require('es5-ext/object/is-callable')
, toPosInt = require('es5-ext/number/to-pos-integer')
, deferred = require('./deferred');
exports = module.exports = function (timeout, cb) {
if (timeout === false) {
// Cancel monitor
delete deferred._monitor;
delete exports.timeout;
delete exports.callback;
return;
}
exports.timeout = timeout = max(toPosInt(timeout) || 5000, 50);
if (cb == null) {
if ((typeof console !== 'undefined') && console &&
isCallable(console.error)) {
cb = function (e) {
console.error(((e.stack && e.stack.toString()) ||
"Unresolved promise: no stack available"));
};
}
} else {
callable(cb);
}
exports.callback = cb;
deferred._monitor = function () {
var e = new Error("Unresolved promise");
return setTimeout(function () {
if (cb) cb(e);
}, timeout);
};
};