-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
usage.js
97 lines (86 loc) · 2.39 KB
/
usage.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
const O_o = require('./lib')
/* Look ma, no try/catch */
async function usageExample(input) {
try {
const [ errorOne, dataOne ] = await O_o(promiseOne(input))
if (errorOne) {
// Handle business logic errors
console.log('Business logic error', errorOne)
}
/* ignore myPromiseTwo business logic errors by omitting first array item */
const [ , dataTwo ] = await O_o(promiseTwo('other'))
/* handle error or null promise response */
const [ errorThree, dataThree ] = await O_o(promiseThree('other'))
if (errorThree || !dataThree) {
console.log('Business logic error three', errorThree)
}
// Bail/retry if required data missing
if (!dataOne || !dataTwo || !dataThree) {
return 'NOPE'
}
// do stuff with data
console.log('myPromise', dataOne)
console.log('myPromiseTwo', dataTwo)
console.log('myPromiseThree', dataThree)
return {
dataOne,
dataTwo,
dataThree
}
} catch (e) {
// Handle native javascript errors here
console.log('Native JS error', e)
}
}
/* Promises with callback */
function promiseOne(input) {
return new Promise((resolve, reject) => {
callbacker(input, (error, data) => {
if (error) return reject(error)
return resolve(data)
})
})
}
/* Normal promise */
function promiseTwo(value) {
return Promise.resolve(`myPromiseTwo data ${value}`)
}
/* Normal promise */
function promiseThree() {
if (simulateFailure(true)) {
console.log('myPromiseThree error triggered')
return Promise.resolve(
new Error('myPromiseThree business logic error')
)
}
return Promise.resolve('myPromiseThree data')
}
/* Normal callback */
function callbacker(input, cb) {
if (simulateFailure(true)) {
console.log('syntaxError error triggered')
// unknownThing will throw syntaxError
console.log(unknownThing)
}
return cb(null, {
value: businessLogic(input)
})
}
/* Randomly simulate failures */
function simulateFailure(enable) {
if (!enable) return false
return (Math.floor(Math.random() * 10) + 1) < 6
}
function businessLogic(input) {
if (simulateFailure()) {
console.log('businessLogic error triggered')
throw new Error('Business logic error xyz')
}
return `${input} fizzbuzz`
}
/* Run the thing */
usageExample('foobar').then((result) => {
console.log('final result', result)
}).catch((err) => {
console.log('final catch', err)
})