-
Notifications
You must be signed in to change notification settings - Fork 0
/
deferred.js
168 lines (147 loc) · 3.46 KB
/
deferred.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
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* @fileoverview Simple deferred lib.
* @author david@stupid-studio.com (David Adalberth Andersen)
*/
/** Import the Event system */
var Event = require('stupid-event');
/**
* Deferred
* @constructor
*/
function Deferred(opts){
/**
* @define {object} Collection of public methods.
*/
var self = {};
/**
* @define {object} options for the constructor
*/
var opts = opts || {};
/**
* @define {object} A promise object that will be returned
*/
var promise = {};
/**
* @define {object} Event system for controlling events
*/
var event = Event();
/**
* Promise then method
* This is for chaining promis callbacks
* @example promiseFunction().then(
* function(){ // success },
* function(){ // rejected },
* function(){ // notify }
* ).then( ... );
* @param {function} sucess Success callback
* @param {function} reject Reject callback
* @param {function} notify notify callback
* @return {object} Returns the promise object
*/
function promiseThen(success, reject, notify){
/**
* @define {object} Return a new promise
*/
var def = Deferred();
/**
* Resolved promise
* @example example
* @param {string} A string key for the event system
* @param {function} A callback when event is triggered
* @return {object} Returns promise object
*/
event.on('resolve', function(){
/**
* If the success callback returns a promise
* then resolve/reject/notify that returned promise
*/
var promise = success();
if(!promise) return;
promise.success(function(){
/** handle the returned deferred object */
def.resolve();
});
promise.error(function(){
def.reject();
});
promise.notify(function(){
def.notify();
});
});
/**
* If promise is rejected/notify trigger the callback
*/
event.on('reject', function(){
if(reject) reject();
});
event.on('notify', function(){
if(notify) notify();
});
/**
* @return {object} Returns a promise object
*/
return def.promise;
}
/**
* Promise methods
* @example promise.then( //new promise ).then(...)
* @example promise.success(...).error(...).notify(...)
* @param {function} callback A callback for the promise
* @return {object} Return the promise
*/
function promiseSuccess(callback){
event.on('resolve', callback);
return promise;
}
function promiseError(callback){
event.on('reject', callback);
return promise;
}
function promiseNotified(callback){
event.on('notify', callback);
return promise;
}
/**
* Deferred methods to trigger the promise
* @example def.resolve(args)
* @example def.reject(args)
* @example def.notify(args)
*/
function resolve(){
var args = Array.prototype.slice.call(arguments);
event.trigger('resolve', args);
}
function reject(){
var args = Array.prototype.slice.call(arguments);
event.trigger('reject', args);
}
function notify(){
var args = Array.prototype.slice.call(arguments);
event.trigger('notify', args);
}
/**
* Add the promise methods to the promise object
*/
promise.then = promiseThen;
promise.success = promiseSuccess;
promise.error = promiseError;
promise.notify = promiseNotified;
/**
* The promise object
* @public {object}
*/
self.promise = promise;
/**
* Deferred public methods
* @public {function}
*/
self.resolve = resolve;
self.reject = reject;
self.notify = notify;
/**
* @return {object} return public methos
*/
return self;
}
/** @export */
module.exports = Deferred;