1
Fork 0

typescript(option): feat: added a mapAsync helper

Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
Matei Adriel 2019-12-23 13:29:38 +02:00 committed by prescientmoon
parent 467198921d
commit 9236bade0c
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4
2 changed files with 19 additions and 0 deletions

View file

@ -16,3 +16,4 @@ export * from './map'
export * from './toArray'
export * from './toNullable'
export * from './withDefault'
export * from './mapAsync'

View 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
)
}