typescript(option): feat: added a mapAsync helper
Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
parent
467198921d
commit
9236bade0c
|
@ -16,3 +16,4 @@ export * from './map'
|
|||
export * from './toArray'
|
||||
export * from './toNullable'
|
||||
export * from './withDefault'
|
||||
export * from './mapAsync'
|
||||
|
|
18
typescript/option/src/helpers/mapAsync.ts
Normal file
18
typescript/option/src/helpers/mapAsync.ts
Normal file
|
@ -0,0 +1,18 @@
|
|||
import { Option, None, Some } from '../types'
|
||||
import { Mapper } from '../internalTypes'
|
||||
import { match } from './match'
|
||||
|
||||
export const mapAsync = <T, U>(
|
||||
mapper: Mapper<T, Promise<U>>,
|
||||
option: Option<T>
|
||||
) => {
|
||||
return match(
|
||||
async value => {
|
||||
const output = await mapper(value)
|
||||
|
||||
return Some(output)
|
||||
},
|
||||
Promise.resolve(None),
|
||||
option
|
||||
)
|
||||
}
|
Loading…
Reference in a new issue