/
chained-call-boxing.js
46 lines (43 loc) · 1.05 KB
/
chained-call-boxing.js
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
41
42
43
44
45
46
/**
* Created by Philip A Senger on 4/08/15.
*/
var Promise = require("bluebird");
/**
* Chaining, without returning a Promise ( boxing )
* ---
*
* Blue Bird will automatically wrap the a function with a passing-promise.
* However, you forfeit flow control and subsequent results become undefined.
*
*/
var makeBoomBoom = true;
var promise = new Promise(function (resolve, reject) {
console.log('1');
resolve({msg: 'hello'});
});
// Chaining
promise.then(function (result) {
console.log('2', result);
result.newmsg = "good by ";
})
.then(function (result) {
console.log('3', result);
})
.then(function (result) {
console.log('4', result);
if (makeBoomBoom) {
throw new Error('Eels in my hovercraft');
}
})
.then(function (result) {
console.log('5', result);
})
.then(function (result) {
console.log('6', result);
})
.catch(function (e) {
console.error('error', e);
})
.finally(function () {
console.log('fini');
});