-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
62 lines (55 loc) · 1.66 KB
/
index.js
File metadata and controls
62 lines (55 loc) · 1.66 KB
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
var AsyncArray = (function(){
function promisify(){
function promise(fn, method) {
new Promise((resolve, reject) => {
Promise.all(this._data.map(fn)).then(values => {
this._data = method(this._data, values);
this.chains.shift();
if (this.chains.length > 0) {
this.chains[0].call();
}
resolve([]);
}).catch(error => {
resolve([]);
});
});
}
this.chains.push(promise.bind(this, ...arguments));
singleMethodCall.call(this);
}
function singleMethodCall(){
if (this.chains.length === 1) {
this.chains[0].call();
}
}
function AsyncArray(data) {
if(!(this instanceof AsyncArray)){
return new AsyncArray(data);
}
this._data = data;
this.chains = [];
}
AsyncArray.prototype.filter = function (fn) {
promisify.call(this, fn,function (input, values){
return input.filter(function (value, index){
return values[index];
});
});
return this;
};
AsyncArray.prototype.map = function (fn) {
promisify.call(this, fn,function(input, values){
return input.map(function (value, index){
return values[index];
});
});
return this;
};
AsyncArray.prototype.forEach = function (fn) {
promisify.call(this, fn,function (input){
return input;
});
return this;
};
return AsyncArray;
})();