Skip to content

Commit bc0b608

Browse files
committed
Refactor every, everyStrict, filter, filterStrict, some, someStrict not to use async
1 parent 01e1f58 commit bc0b608

File tree

6 files changed

+52
-50
lines changed

6 files changed

+52
-50
lines changed

src/every.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import asyncForEach from './forEach';
33
export default function asyncSome(arr, fn) {
44
return new Promise((resolve) => {
55
// eslint-disable-next-line no-shadow
6-
asyncForEach(arr, async (cur, idx, arr) => {
7-
const result = await fn(cur, idx, arr);
8-
9-
if (!result) {
10-
resolve(false);
11-
}
12-
}).then(() => {
13-
resolve(true);
14-
});
6+
asyncForEach(arr, (cur, idx, arr) => new Promise((resolve2) => {
7+
fn(cur, idx, arr).then((result) => {
8+
if (!result) {
9+
resolve(false);
10+
}
11+
resolve2();
12+
});
13+
}))
14+
.then(() => { resolve(true); });
1515
});
1616
}

src/every_strict.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import asyncForEachStrict from './forEach_strict';
33
export default function asyncEveryStrict(arr, fn) {
44
return new Promise((resolve) => {
55
// eslint-disable-next-line no-shadow
6-
asyncForEachStrict(arr, async (cur, idx, arr) => {
7-
const result = await fn(cur, idx, arr);
8-
9-
if (!result) {
10-
resolve(false);
11-
}
12-
}).then(() => {
13-
resolve(true);
14-
});
6+
asyncForEachStrict(arr, (cur, idx, arr) => new Promise((resolve2) => {
7+
fn(cur, idx, arr).then((result) => {
8+
if (!result) {
9+
resolve(false);
10+
}
11+
resolve2();
12+
});
13+
}))
14+
.then(() => { resolve(true); });
1515
});
1616
}

src/filter.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ export default function asyncSome(arr, fn) {
44
const result = [];
55

66
// eslint-disable-next-line no-shadow
7-
return asyncForEach(arr, async (cur, idx, arr) => {
8-
const cond = await fn(cur, idx, arr);
9-
10-
if (cond) {
11-
result[idx] = cur;
12-
}
13-
})
7+
return asyncForEach(arr, (cur, idx, arr) => new Promise((resolve) => {
8+
fn(cur, idx, arr).then((cond) => {
9+
if (cond) {
10+
result[idx] = cur;
11+
}
12+
resolve();
13+
});
14+
}))
1415
.then(() => result.filter((cur, idx) => idx in result));
1516
}

src/filter_strict.js

+8-7
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ export default function asyncFilterStrict(arr, fn) {
44
const result = [];
55

66
// eslint-disable-next-line no-shadow
7-
return asyncForEachStrict(arr, async (cur, idx, arr) => {
8-
const cond = await fn(cur, idx, arr);
9-
10-
if (cond) {
11-
result[idx] = cur;
12-
}
13-
})
7+
return asyncForEachStrict(arr, (cur, idx, arr) => new Promise((resolve) => {
8+
fn(cur, idx, arr).then((cond) => {
9+
if (cond) {
10+
result[idx] = cur;
11+
}
12+
resolve();
13+
});
14+
}))
1415
.then(() => result.filter((cur, idx) => idx in result));
1516
}

src/some.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import asyncForEach from './forEach';
33
export default function asyncSome(arr, fn) {
44
return new Promise((resolve) => {
55
// eslint-disable-next-line no-shadow
6-
asyncForEach(arr, async (cur, idx, arr) => {
7-
const result = await fn(cur, idx, arr);
8-
9-
if (result) {
10-
resolve(true);
11-
}
12-
}).then(() => {
13-
resolve(false);
14-
});
6+
asyncForEach(arr, (cur, idx, arr) => new Promise((resolve2) => {
7+
fn(cur, idx, arr).then((result) => {
8+
if (result) {
9+
resolve(true);
10+
}
11+
resolve2();
12+
});
13+
}))
14+
.then(() => { resolve(false); });
1515
});
1616
}

src/some_strict.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import asyncForEachStrict from './forEach_strict';
33
export default function asyncSomeStrict(arr, fn) {
44
return new Promise((resolve) => {
55
// eslint-disable-next-line no-shadow
6-
asyncForEachStrict(arr, async (cur, idx, arr) => {
7-
const result = await fn(cur, idx, arr);
8-
9-
if (result) {
10-
resolve(true);
11-
}
12-
}).then(() => {
13-
resolve(false);
14-
});
6+
asyncForEachStrict(arr, (cur, idx, arr) => new Promise((resolve2) => {
7+
fn(cur, idx, arr).then((result) => {
8+
if (result) {
9+
resolve(true);
10+
}
11+
resolve2();
12+
});
13+
}))
14+
.then(() => { resolve(false); });
1515
});
1616
}

0 commit comments

Comments
 (0)