-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
37 lines (32 loc) · 1016 Bytes
/
index.ts
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
export {};
declare global {
interface Array<T> {
syncForEach(any,ending_function?): Array<T>;
}
}
Array.prototype.syncForEach = function<T>(this: T[], callback: any,ending_function?:any): any {
return new Promise<any>((resolve, reject) => {
let index = -1;
let resolved = false
const next = () => {
index++;
if (this.length > index) {
if (this.length > 0) {
callback(this[index], next, index+1, this.length,finish);
}
}else{
if (!resolved) {
resolve(ending_function ? ending_function : true);
if (ending_function) ending_function();
}
}
}
const finish = () => {
resolved = true;
index = this.length;
resolve(ending_function ? ending_function : true);
if (ending_function) ending_function();
}
next();
});
}