
var co = require('co');

module.exports = function(timeout) {
  return function *(next) {
    var ctx = this;
    yield Promise.race([
      new Promise(function(resolve, reject) {
        setTimeout(function() {
          var e = new Error('Request timeout')
          e.status = 408;
          reject(e);
        }, timeout);
      }),
      new Promise(function(resolve, reject) {
        co(function*() {
          yield *next;
        }).call(ctx, function(err) {
          if(err) reject(err);
          resolve();
        });
      })
    ]);
  };
};


var co = require('co');

module.exports = function(timeout) {
  return function *(next) {
    var ctx = this;
    var tmr = null;
    yield Promise.race([
      new Promise(function(resolve, reject) {
        tmr = setTimeout(function() {
          var e = new Error('Request timeout');
          e.status = 408;
          reject(e);
        }, timeout);
      }),
      new Promise(function(resolve, reject) {
        co(function*() {
          yield *next;
        }).call(ctx, function(err) {
          clearTimeout(tmr);
          if(err) reject(err);
          resolve();
        });
      })
    ]);
  };
};

Clear the timeout when the request does not timeout
Clear the timeout when the request does not timeout
