-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
155 lines (141 loc) · 3.5 KB
/
index.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*!
* dush-promise <https://github.com/tunnckoCore/dush-promise>
*
* Copyright (c) Charlike Mike Reagent <@tunnckoCore> (https://i.am.charlike.online)
* Released under the MIT license.
*/
'use strict'
var extend = require('extend-shallow')
var Deferred = require('native-promise-deferred')
/**
* > Adds a Promise methods such as `.resolve`, `.reject`
* `.then` and `.catch` to your [dush][] application. Useful
* from inside plugins. This plugin also emits `error` event
* when `app.reject` is used.
*
* **Example**
*
* ```js
* var dush = require('dush')
* var promise = require('dush-promise')
*
* var app = dush().use(promise())
*
* console.log(app.then)
* console.log(app.catch)
* console.log(app.reject)
* console.log(app.resolve)
* ```
*
* @param {Object} `opts` optional, passed directly to [native-promise-deferred][]
* @return {Function} a plugin function that should be passed to `.use` method of [dush][]
* @api public
*/
module.exports = function dushPromise (opts) {
return function dushPromise_ (app) {
var promise = Deferred(extend({}, app.options, opts))
/**
* > Handle resolved promise with `onresolved` or rejected promise
* with `onrejected`. It is as any usual Promise `.then` method.
*
* **Example**
*
* ```js
* app.then((res) => {
* console.log(res) // => 123
* })
* app.resolve(123)
*
* // or handle rejected promise
* app.then(null, (er) => {
* console.log('err!', er) // => Error: foo bar
* })
* app.reject(new Error('foo bar'))
* ```
*
* @name .then
* @param {Function} `onresolved`
* @param {Function} `onrejected`
* @return {Promise}
* @api public
*/
app.then = function then (onresolved, onrejected) {
return promise.then(onresolved, onrejected)
}
/**
* > Catch a rejected promise error. This method is mirror
* of any usual promise `.catch` method.
*
* **Example**
*
* ```js
* app.on('error', (err) => {
* console.log('er!', err) // => Error: sad err
* })
* app.catch((err) => {
* console.log('oops, error!', err) // => Error: sad err
* })
*
* app.reject(new Error('sad err'))
* ```
*
* @name .catch
* @param {Function} `onrejected`
* @return {Promise}
* @api public
*/
app.catch = function catch_ (onrejected) {
return promise.catch(onrejected)
}
/**
* > As any usual `Promise.resolve` method.
*
* **Example**
*
* ```js
* app.use((app) => {
* app.foo = () => {
* return app.resolve(1222)
* }
* })
*
* const promise = app.foo()
* promise.then((val) => {
* console.log('res:', val) // => 1222
* })
* ```
*
* @name .resolve
* @param {any} `val`
* @return {Promise}
* @api public
*/
app.resolve = function resolve (val) {
return promise.resolve(val)
}
/**
* > As any usual `Promise.reject` method.
*
* **Example**
*
* ```js
* app.on('error', (err) => {
* console.log('some error:', err) // => Error: quxie
* })
* app.reject(new Error('quxie'))
* ```
*
* @name .reject
* @param {Error} `err`
* @return {Promise}
* @api public
*/
app.reject = function reject (err) {
return promise.reject(err)
}
app.catch(function onRejected (er) {
app.emit('error', er)
})
return app
}
}