-
Notifications
You must be signed in to change notification settings - Fork 0
/
sandal-promise.js
48 lines (43 loc) · 1.25 KB
/
sandal-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
(function () {
'use strict';
var sandalPromise = function (_q) {
if (!_q) throw new Error('Can find window.Q. Include the q promises library before sandal-promise');
return function (sandal, isConstructor) {
(isConstructor ? sandal.prototype : sandal).promise = function (components) {
var components = [];
for (var i = 0; i < arguments.length; i++) {
components.push(arguments[i]);
}
var resolveOne = false;
if (components.length === 1) {
resolveOne = true;
components = [components];
}
var defer = _q.defer();
(isConstructor ? this : sandal).resolve(components, function () {
if (arguments[0]) {
defer.reject(arguments[0]);
return;
}
var result = [];
if (resolveOne) {
result = arguments[1];
} else {
for (var j = 0; j < components.length; j++) {
result.push(arguments[j + 1]);
}
}
defer.resolve(result);
});
return defer.promise;
};
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = sandalPromise(require('q'));
}
if (typeof window !== 'undefined') {
if (!window.Sandal) throw new Error('Sandal must be loaded before sandal-promise');
window.Sandal.extend(sandalPromise(window.Q));
}
})();