Debian Codegraph @

deferred/monitor.js

raw
1 /* eslint no-console: "off" */
2
3 // Run if you want to monitor unresolved promises (in properly working
4 // application there should be no promises that are never resolved)
5
6 "use strict";
7
8 var max = Math.max
9 , isValue = require("es5-ext/object/is-value")
10 , callable = require("es5-ext/object/valid-callable")
11 , isCallable = require("es5-ext/object/is-callable")
12 , toPosInt = require("es5-ext/number/to-pos-integer")
13 , deferred = require("./deferred");
14
15 exports = module.exports = function (timeout, cb) {
16 if (timeout === false) {
17 // Cancel monitor
18 delete deferred._monitor;
19 delete exports.timeout;
20 delete exports.callback;
21 return;
22 }
23 exports.timeout = timeout = max(toPosInt(timeout) || 5000, 50);
24 if (isValue(cb)) {
25 callable(cb);
26 } else if (typeof console !== "undefined" && console && isCallable(console.error)) {
27 cb = function (e) {
28 console.error(
29 (e.stack && e.stack.toString()) || "Unresolved promise: no stack available"
30 );
31 };
32 }
33 exports.callback = cb;
34
35 deferred._monitor = function () {
36 var e = new Error("Unresolved promise");
37 return setTimeout(function () { if (cb) cb(e); }, timeout);
38 };
39 };