1 | 'use strict';
|
2 |
|
3 | var Promise = require('promise');
|
4 | var ms = require('ms');
|
5 |
|
6 | module.exports = timeout;
|
7 | function timeout(val, timeout) {
|
8 | if (timeout === Infinity || timeout === null) {
|
9 | return Promise.resolve(val);
|
10 | }
|
11 | return new Promise(function (resolve, reject) {
|
12 | var timer = setTimeout(function () {
|
13 | reject(new Error('Operation timed out'))
|
14 | }, ms(timeout.toString()))
|
15 | Promise.resolve(val)
|
16 | .done(function (res) {
|
17 | clearTimeout(timer)
|
18 | resolve(res)
|
19 | }, function (err) {
|
20 | clearTimeout(timer)
|
21 | reject(err)
|
22 | })
|
23 | })
|
24 | }
|