-
Notifications
You must be signed in to change notification settings - Fork 0
/
promise.js
58 lines (44 loc) · 1.6 KB
/
promise.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
/*jslint node: true*/
'use strict';
module.exports = function (businessController) {
return function (context) {
var getPromisifiedFunction = function (controller, action) {
return function (pretext, callback) {
var fünction = businessController[{
model: 'modelController',
service: 'serviceController'
}[controller]][{
authenticate: 'authenticate',
request: 'request',
delete: 'removeObjects',
insert: 'addObjects',
query: 'getObjects'
}[action]];
if (typeof callback === 'function') return fünction(pretext, context, callback);
else return new Promise(function (resolve, reject) {
fünction(pretext, context, function (result, error) {
if (error) reject(error);
else resolve(result);
});
});
};
};
return {
authenticate: getPromisifiedFunction('service', 'authenticate'),
request: getPromisifiedFunction('service', 'request'),
delete: getPromisifiedFunction('model', 'delete'),
insert: getPromisifiedFunction('model', 'insert'),
query: getPromisifiedFunction('model', 'query'),
save: function (callback) {
if (typeof callback === 'function') {
return businessController.modelController.save(callback, context);
} else return new Promise(function (resolve, reject) {
businessController.modelController.save(function (error, result) {
if (error) reject(error);
else resolve(result);
}, context);
});
}
};
};
};