/
promises.js
56 lines (49 loc) · 1.28 KB
/
promises.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
var breeze = require('../breeze')
// Helpers
var noop = function () {}
// Generic failure promise
var ErrorPromise = {
then: function () {
return {
catch: function (next) {
next(':(')
}
}
},
catch: function (next) {
next(':(')
}
}
// Generic successful promise.
var SuccessPromise = {
then: function (next) {
next(':)')
return { catch: noop }
},
catch: noop
}
// Old Generic successful promise
var OldSuccessPromise = {
then: function (success, error) {
success(':)')
}
}
// Initialize flow system
//
// Here we return our promise directly, since promises have their own error / success system we let
// breeze determine the path from the promise results.
//
// Swap out `SuccessPromise` for `ErrorPromise` if you want to see a sad-face.
// Swap out `SuccessPromise` for `OldSuccessPromise` if you want to see breeze recognize older promise style.
var flow = breeze(function (next) {
next(SuccessPromise, ';^)')
})
// When promise is successful
flow.then(function (next, passedValue, promiseValue) {
console.log('Passed values should come first ";^)":', passedValue)
console.log('Promise should pass with ":)":', promiseValue)
})
// When promise fails
flow.catch(function (err) {
console.log('Promise should fail with ":(":', err)
})