typescript(option): feat: added a mapAsync helper
Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
		
					parent
					
						
							
								467198921d
							
						
					
				
			
			
				commit
				
					
						9236bade0c
					
				
			
		
					 2 changed files with 19 additions and 0 deletions
				
			
		typescript/option/src/helpers
|  | @ -16,3 +16,4 @@ export * from './map' | ||||||
| export * from './toArray' | export * from './toArray' | ||||||
| export * from './toNullable' | export * from './toNullable' | ||||||
| export * from './withDefault' | 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…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Matei Adriel
				Matei Adriel