Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Promise monitor should now be configured through dedicated module

  • Loading branch information...
commit d3b8c763806d9ccb2dd73ddaccffca8eb50f1379 1 parent 6212bcc
@medikoo authored
View
1  lib/index.js
@@ -11,6 +11,7 @@ module.exports = merge(require('./deferred'), {
isPromise: require('./is-promise'),
delay: call.bind(require('./ext/function/delay')),
gate: call.bind(require('./ext/function/gate')),
+ monitor: require('./monitor'),
promisify: call.bind(require('./ext/function/promisify')),
promisifyAsync: call.bind(require('./ext/function/promisify-async')),
promisifySync: call.bind(require('./ext/function/promisify-sync')),
View
39 lib/monitor.js
@@ -0,0 +1,39 @@
+// Run if you want to monitor unresolved promises (in properly working
+// application there should not be promises that are never resolved)
+
+var max = Math.max
+ , assertCallable = require('es5-ext/lib/Object/assert-callable')
+ , isCallable = require('es5-ext/lib/Object/is-callable')
+ , toUinteger = require('es5-ext/lib/Number/to-uinteger')
+ , deferred = require('./deferred')
+ , promise = require('./promise');
+
+exports = module.exports = function (timeout, cb) {
+ if (timeout === false) {
+ // Cancel monitor
+ delete promise.monitor;
+ delete exports.timeout;
+ delete exports.callback;
+ return;
+ }
+ exports.timeout = timeout = max(toUinteger(timeout) || 5000, 50);
+ if (cb == null) {
+ if ((typeof console !== 'undefined') && console &&
+ isCallable(console.error)) {
+ cb = function (e) {
+ console.error("UNRESOLVED PROMISE:" +
+ ((e.stack && e.stack.toString()) || 'no stack available'));
+ };
+ }
+ } else {
+ assertCallable(cb);
+ }
+ exports.callback = cb;
+
+ promise.monitor = function () {
+ var e = new Error("Unresolved promise");
+ return setTimeout(function () {
+ cb && cb(e);
+ }, timeout);
+ };
+};
View
2  lib/promise.js
@@ -91,7 +91,7 @@ Unresolved = function () {
this.pending = [];
this.promises = [];
this.timeout = setTimeout(noop, 1e13);
- this.monitor = deferred.MONITOR && deferred.MONITOR();
+ this.monitor = createPromise.monitor && createPromise.monitor();
};
Unresolved.prototype = {
resolved: false,
View
20 test/monitor.js
@@ -0,0 +1,20 @@
+'use strict';
+
+var deferred = require('../lib/deferred');
+
+module.exports = function (t, a, d) {
+ var invoked, df, cachet, cachec;
+ cachet = t.timeout;
+ cachec = t.callback
+ t(100, function (stack) {
+ a.ok(stack instanceof Error);
+ invoked = true;
+ });
+ df = deferred();
+ setTimeout(function () {
+ a(invoked, true, "Invoked");
+ t(cachet, cachec);
+ df.resolve();
+ d();
+ }, 150);
+};
Please sign in to comment.
Something went wrong with that request. Please try again.