-
Notifications
You must be signed in to change notification settings - Fork 0
/
Async.js
108 lines (99 loc) · 3.53 KB
/
Async.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
'use strict';
hwc.define([
'hwc!{PATH_JS_LIB}common/include.js'
], function () {
var $ = this;
/**
* "duck punching" RSVP
*
*/
/*
* when calling .then() function from cb*() methods
* latest argument is be te callback result
* if not callback defined it's null
*/
$.RSVP.defer.prototype.cbResolve = function () {
if (this.promise === null && this._callback) {
return this._callback.apply(null, arguments);
} else {
this.resolve(arguments);
}
};
$.RSVP.defer.prototype.cbReject = function () {
if (this.promise === null && this._callback) {
return this._callback.apply(null, arguments);
} else {
this.reject(arguments);
}
};
var tmp = $.Promise.prototype["catch"];
$.Promise.prototype["catch"] = function (err, label) {
// add marker to notify that exception has been caught
this._label = "__caught__" + (this._label || "");
return tmp.call(this, err, label);
};
// compatible alias for catch
$.Promise.prototype.fail = $.Promise.prototype["catch"];
$.RSVP.configure('instrument', true);
$.RSVP.configure('instrument-with-stack', true);
$.RSVP.on('rejected', function (e) {
// print error only when exception has not been caught using promise methods
if (!e.label || !e.label.indexOf("__caught__") === 0) {
console.error("Uncaught exception", e, e.detail.stack);
}
});
/*
* Adapter class for some Q methods
*/
return $.Async = $.Class({members: [
{
a: ["public", "static"], n: "all", v: function (promises) {
return $.Promise.all(promises);
}
},
{
/**
* array of promising-function that should be called sequentially
*/
a: ["public", "static"], n: "sequence", v: function (fnArray) {
return fnArray.slice(1).reduce(function (prev, curr) {
return prev.then(curr);
}, fnArray[0]());
}
},
/*{
a: ["public", "static"], n: "call", v: function (fn) {
return this.s.apply(fn, Array.prototype.slice.call(arguments, 1));
}
},
{
a: ["public", "static"], n: "apply", v: function (fn, args) {
return $.Q.nfapply(fn, args);
}
},*/
{
/**
* fn: accepts resolve and reject methods as parameter
*/
a: ["public", "static"], n: "promise", v: function (fn) {
return new $.Promise(fn);
}
},
{
/**
* if callback is defined then promise will not be set
*/
a: ["public", "static"], n: "defer", v: function (callback) {
var deferred = $.RSVP.defer();
if (callback) {
if (typeof callback !== "function")
throw new Error("callback type is: " + typeof (callback));
deferred._callback = callback;
deferred.promise = null;
}
return deferred;
}
}
]}
);
});