-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmimicked-delay.js
More file actions
40 lines (35 loc) · 969 Bytes
/
mimicked-delay.js
File metadata and controls
40 lines (35 loc) · 969 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var Q = require("q");
var first = function () {
var d = Q.defer();
console.log('First one');
setTimeout(d.resolve, 1000);
return d.promise;
};
var second = function () {
var d = Q.defer();
console.log('Second one');
setTimeout(d.resolve, 1500);
return d.promise;
};
// This fuction throws an error which later on we show will be handled
var third = function () {
var d = Q.defer();
console.log('Third one');
setTimeout(d.resolve, 2500);
throw new Error('Awww sheeeeiiiit');
};
// This function will not be reached because the previous one is going to fall over.
var fourth = function () {
var d = Q.defer();
console.log('Fourth one');
setTimeout(d.resolve, 2500);
return d.promise;
};
first()
.then(second)
.then(third)
.then(fourth, function (error) {
console.log('Something went wrong in 1-3: ' + error.message);
})
// We are not returning the promise chain, so we need to call done() to ensure unhandled errors are rethrown
.done();