-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path8-chain.js
More file actions
26 lines (22 loc) · 572 Bytes
/
Copy path8-chain.js
File metadata and controls
26 lines (22 loc) · 572 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
'use strict';
const getNumbers = () => ({
numbers: [1, 2, 3],
then(onFulfilled, onRejected) {
const num = this.numbers.shift();
if (num) {
onFulfilled(num);
} else {
onRejected(new Error('I have no numbers for you'));
}
return this; // chain thenable
},
});
// Usage
const onSuccess = (res) => console.dir({ res });
const onError = (err) => console.dir({ err: err.message });
getNumbers()
.then(onSuccess, onError)
.then(onSuccess, onError)
.then(onSuccess, onError)
.then(onSuccess, onError)
.then(onSuccess, onError);