Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Add q-timeout to promises with either a value or callback
Example: timeout with message, just like Q.promise.timeout
.timeout(500, 'promise timed out!')
.then(function () {
console.log('foo has been resolved!');
.fail(function (msg) {
console.log('failed with value:', msg);
Example: timeout with callback, reject deferred if you want
.timeout(500, function (defer) {
console.log('timed out callback');
.fail(function (msg) {
if (msg)
console.log('failed with value:', msg);
var installed = false;
module.exports = function (q) {
(function addPromiseTimeoutSupport(q) {
if (installed) {
var _defer = q.defer;
q.defer = function () {
var d = _defer();
d.promise.timeout = function (ms, timedOutValue) {
setTimeout(function () {
if (typeof timedOutValue === 'function') {
} else {
}, ms);
return d.promise;
return d;
installed = true;

This comment has been minimized.

Copy link
Owner Author

bahmutov commented Jun 18, 2014

I always wanted to run a callback on promise timeout. Q.promise.timeout is almost what I needed, this adds ability to run a dedicated callback. One still needs to detect if the promise was rejected due to timeout in .fail callback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.