Skip to content

Commit

Permalink
fix(Async): use for of loop instead of reduce in async fns
Browse files Browse the repository at this point in the history
  • Loading branch information
Thanga-Ganapathy committed Apr 4, 2024
1 parent 009e418 commit 9b23586
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 88 deletions.
5 changes: 5 additions & 0 deletions .changeset/wise-cows-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@opentf/std": minor
---

Fixed async fns not awaiting within cb fn.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ await sleep(1000); // It suspends the exection for 1 second.

## Async

- [asyncFilter](https://js-std.pages.dev/Array/asyncFilter)
- [asyncForEach](https://js-std.pages.dev/Array/asyncForEach)
- [asyncMap](https://js-std.pages.dev/Array/asyncMap)
- [aFilter](https://js-std.pages.dev/Async/aFilter)
- [aForEach](https://js-std.pages.dev/Async/aForEach)
- [aMap](https://js-std.pages.dev/Async/aMap)

### Maths

Expand Down
6 changes: 3 additions & 3 deletions apps/docs/pages/Async/_meta.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"asyncFilter": "asyncFilter",
"asyncForEach": "asyncForEach",
"asyncMap": "asyncMap"
"aFilter": "aFilter",
"aForEach": "aForEach",
"aMap": "aMap"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import REPL from "../../components/REPL";
## Syntax

```ts
asyncFilter(
aFilter(
arr: T[],
cb: (value: T, index: number) => Promise<boolean>
): Promise<Partial<T[]>>
Expand All @@ -14,9 +14,9 @@ asyncFilter(
## Usage

```ts
import { asyncFilter } from "@opentf/std";
import { aFilter } from "@opentf/std";

asyncFilter([], async (value, index) => {});
aFilter([], async (value, index) => {});
```

## Examples
Expand All @@ -30,14 +30,14 @@ function isEven(n) {
});
}

const filteredArr = await asyncFilter(arr, async (n) => await isEven(n));
const filteredArr = await aFilter(arr, async (n) => await isEven(n));

console.log(filteredArr); //=> [2, 4]
```

## Try

<REPL code={`const { asyncFilter } = require('@opentf/std');
<REPL code={`const { aFilter } = require('@opentf/std');
function isEven(n) {
return new Promise((resolve) => {
Expand All @@ -47,7 +47,7 @@ function isEven(n) {
async function main() {
const arr = [1, 2, 3, 4, 5];
const out = await asyncFilter(arr, async (n) => await isEven(n));
const out = await aFilter(arr, async (n) => await isEven(n));
log(out);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import REPL from "../../components/REPL";
## Syntax

```ts
asyncForEach(
aForEach(
arr: T[],
cb: (value: T, index: number) => Promise<void>
): Promise<undefined>
Expand All @@ -14,9 +14,9 @@ asyncForEach(
## Usage

```ts
import { asyncForEach } from "@opentf/std";
import { aForEach } from "@opentf/std";

asyncForEach([], async (value, index) => {});
aForEach([], async (value, index) => {});
```

## Examples
Expand All @@ -30,7 +30,7 @@ const flatten = async (val) => {

const nested = [1, 2, 3, [4, 5, [6, 7], 8, 9]];
const tmpArr = [];
await asyncForEach(nested, async (n) => {
await aForEach(nested, async (n) => {
const val = await flatten(n);
Array.isArray(val) ? tmpArr.push(...val) : tmpArr.push(val);
});
Expand All @@ -39,7 +39,7 @@ console.log(tmpArr) // [1, 2, 3, 4, 5, 6, 7, 8, 9]

## Try

<REPL code={`const { asyncForEach } = require('@opentf/std');
<REPL code={`const { aForEach } = require('@opentf/std');
const flatten = async (val) => {
return new Promise((resolve) => {
Expand All @@ -51,7 +51,7 @@ const flatten = async (val) => {
async function main() {
const nested = [1, 2, 3, [4, 5, [6, 7], 8, 9]];
const tmpArr = [];
await asyncForEach(nested, async (n) => {
await aForEach(nested, async (n) => {
const val = await flatten(n);
Array.isArray(val) ? tmpArr.push(...val) : tmpArr.push(val);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import REPL from "../../components/REPL";
## Syntax

```ts
asyncMap(
aMap(
arr: T[],
cb: (value: T, index: number) => Promise<unknown>
): Promise<unknown[]>
Expand All @@ -14,9 +14,9 @@ asyncMap(
## Usage

```ts
import { asyncMap } from "@opentf/std";
import { aMap } from "@opentf/std";

asyncMap([], async (value, index) => {});
aMap([], async (value, index) => {});
```

## Examples
Expand All @@ -30,13 +30,13 @@ function multiply(n, i) {
});
}

const result = await asyncMap(arr, async (n, i) => await multiply(n, i));
const result = await aMap(arr, async (n, i) => await multiply(n, i));
console.log(result); // [0, 2, 6, 12, 20]
```

## Try

<REPL code={`const { asyncMap } = require('@opentf/std');
<REPL code={`const { aMap } = require('@opentf/std');
function multiply(n, i) {
return new Promise((resolve) => {
Expand All @@ -47,7 +47,7 @@ function multiply(n, i) {
async function main() {
const arr = [1, 2, 3, 4, 5];
const result = await asyncMap(arr, async (n, i) => await multiply(n, i));
const result = await aMap(arr, async (n, i) => await multiply(n, i));
log(result);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/std/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ await sleep(1000); // It suspends the exection for 1 second.

## Async

- [asyncFilter](https://js-std.pages.dev/Array/asyncFilter)
- [asyncForEach](https://js-std.pages.dev/Array/asyncForEach)
- [asyncMap](https://js-std.pages.dev/Array/asyncMap)
- [aFilter](https://js-std.pages.dev/Async/aFilter)
- [aForEach](https://js-std.pages.dev/Async/aForEach)
- [aMap](https://js-std.pages.dev/Async/aMap)

### Maths

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { asyncFilter } from '../../src';
import { aFilter } from '../../src';

describe('Array', () => {
test('asyncFilter', async () => {
describe('Array > aFilter', () => {
test('filters only even numbers', async () => {
const arr = [1, 2, 3, 4, 5];

function isEven(n) {
Expand All @@ -10,7 +10,7 @@ describe('Array', () => {
});
}

const filteredArr = await asyncFilter(arr, async (n) => await isEven(n));
const filteredArr = await aFilter(arr, async (n) => await isEven(n));

expect(filteredArr).toEqual([2, 4]);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { asyncForEach } from '../../src';
import { aForEach } from '../../src';

describe('Array', () => {
test('asyncForEach', async () => {
describe('Array > aForEach', () => {
test('async fn in cb fn', async () => {
const flatten = async (val) => {
return new Promise((resolve) => {
Array.isArray(val) ? resolve(val.flat()) : resolve(val);
Expand All @@ -10,10 +10,17 @@ describe('Array', () => {

const nested = [1, 2, 3, [4, 5, [6, 7], 8, 9]];
const tmpArr = [];
await asyncForEach(nested, async (n) => {
await aForEach(nested, async (n) => {
const val = await flatten(n);
Array.isArray(val) ? tmpArr.push(...val) : tmpArr.push(val);
});
expect(tmpArr).toEqual([1, 2, 3, 4, 5, 6, 7, 8, 9]);
});

test('cb fn index', async () => {
const arr = [1, 2, 3];
const out = [];
await aForEach(arr, (_e, i) => out.push(i));
expect(out).toEqual([0, 1, 2]);
});
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { asyncMap } from '../../src';
import { aMap, sleep } from '../../src';

describe('Array', () => {
test('asyncMap', async () => {
describe('Array > aMap', () => {
test('async map', async () => {
const arr = [1, 2, 3, 4, 5];

function multiply(n, i) {
Expand All @@ -10,7 +10,8 @@ describe('Array', () => {
});
}

const result = await asyncMap(arr, async (n, i) => await multiply(n, i));
const result = await aMap(arr, async (n, i) => await multiply(n, i));

expect(result).toEqual([0, 2, 6, 12, 20]);
});
});
24 changes: 24 additions & 0 deletions packages/std/src/async/aFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Array filter with `Async` callback function.
*
* @example
*
* const arr = [1, 2, 3]
* aFilter(arr, (n) => isOdd(n)) //=> [1, 3]
*/
export default async function aFilter<T>(
arr: T[],
cb: (value: T, index: number) => Promise<boolean>
): Promise<Partial<T[]>> {
let i = 0;
const out = [];

for (const e of arr) {
if (await cb(e, i)) {
out.push(e);
}
i++;
}

return out;
}
18 changes: 18 additions & 0 deletions packages/std/src/async/aForEach.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Array forEach with `Async` callback function.
*
* @example
*
* await aForEach([1, 3, 5], async () => await someDelayedFn())
*/
export default async function aForEach<T>(
arr: T[],
cb: (value: T, index: number) => Promise<void>
): Promise<undefined> {
let i = 0;

for (const e of arr) {
await cb(e, i);
i++;
}
}
20 changes: 20 additions & 0 deletions packages/std/src/async/aMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Array map with `Async` callback function.
*
* @example
* await aMap([1, 2, 3], (n) => await someDelayedFn(n))
*/
export default async function aMap<T>(
arr: T[],
cb: (value: T, index: number) => Promise<unknown>
): Promise<unknown[]> {
let i = 0;
const out: unknown[] = [];

for (const e of arr) {
out.push(await cb(e, i));
i++;
}

return out;
}
16 changes: 0 additions & 16 deletions packages/std/src/async/asyncFilter.ts

This file was deleted.

15 changes: 0 additions & 15 deletions packages/std/src/async/asyncForEach.ts

This file was deleted.

15 changes: 0 additions & 15 deletions packages/std/src/async/asyncMap.ts

This file was deleted.

6 changes: 3 additions & 3 deletions packages/std/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export { default as drop } from './array/drop';
export { default as intersperse } from './array/intersperse';

// Async
export { default as asyncFilter } from './async/asyncFilter';
export { default as asyncMap } from './async/asyncMap';
export { default as asyncForEach } from './async/asyncForEach';
export { default as aFilter } from './async/aFilter';
export { default as aMap } from './async/aMap';
export { default as aForEach } from './async/aForEach';

// Maths
export { default as percentage } from './maths/percentage';
Expand Down

0 comments on commit 9b23586

Please sign in to comment.