Skip to content
This repository has been archived by the owner on Aug 28, 2024. It is now read-only.

Commit

Permalink
feat: added new async methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tmgulland committed May 31, 2024
1 parent c635a53 commit a893bd4
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@
"require": "./dist/src/object.js",
"import": "./dist/src/object.js"
},
"./promise": {
"types": "./dist/promise.d.ts",
"require": "./dist/src/promise.js",
"import": "./dist/src/promise.js"
"./async": {
"types": "./dist/async.d.ts",
"require": "./dist/src/async.js",
"import": "./dist/src/async.js"
},
"./time": {
"types": "./dist/time.d.ts",
Expand Down
2 changes: 1 addition & 1 deletion scripts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ await Bun.build({
'./src/map.ts',
'./src/merge.ts',
'./src/object.ts',
'./src/promise.ts',
'./src/async.ts',
'./src/time.ts',
'./src/equals.ts',
'./src/clone.ts'
Expand Down
8 changes: 8 additions & 0 deletions src/promise.ts → src/async.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ export const settle = async <T>(promises: Promise<T>[]) => {
const results = await Promise.allSettled(promises)
return parseSettled(results)
}

export async function collect<T>(asyncGenerator: AsyncGenerator<T>): Promise<T[]> {
const result: T[] = []
for await (const item of asyncGenerator) {
result.push(item)
}
return result
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ export {
export { sortMapToArray, NiceMap } from './map'
export { getTimeSince } from './time'
export { type Merge, simpleMerge } from './merge'
export { isFulfilled, isRejected, settle, parseSettled } from './promise'
export { isFulfilled, isRejected, settle, parseSettled } from './async'
export { shallowEquals, simpleEquals, type Equals, arraysEquals } from './equals'
4 changes: 2 additions & 2 deletions test/promise.test.ts → test/async.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, it, expect } from 'bun:test'
import { isFulfilled, parseSettled, settle } from '../src/promise'
import { isFulfilled, parseSettled, settle } from '../src/async'

describe('Promise Utilities', () => {
describe('async utilities', () => {
describe('isFulfilled', () => {
it('returns true for a fulfilled promise result', () => {
const result: PromiseSettledResult<number> = { status: 'fulfilled', value: 42 }
Expand Down

0 comments on commit a893bd4

Please sign in to comment.