- Notifications
You must be signed in to change notification settings - Fork0
functional try-catch wrapper for promises
License
NotificationsYou must be signed in to change notification settings
coderaiser/try-to-catch
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Functionaltry-catch
wrapper forpromises
.
npm i try-to-catch
Wrap function to avoidtry-catch
block, resolves[error, result]
;
Simplest example withasync-await
:
consttryToCatch=require('try-to-catch');constreject=Promise.reject.bind(Promise);awaittryToCatch(reject,'hi');// returns// [ Error: hi]
Can be used with functions:
consttryToCatch=require('try-to-catch');awaittryToCatch(()=>5);// returns[null,5];
Advanced example:
const{readFile, readdir}=require('fs/promises');consttryToCatch=require('try-to-catch');read(process.argv[2]).then(console.log).catch(console.error);asyncfunctionread(path){const[error,data]=awaittryToCatch(readFile,path,'utf8');if(!error)returndata;if(error.code!=='EISDIR')returnerror;returnawaitreaddir(path);}
- try-catch - functional try-catch wrapper.
MIT
About
functional try-catch wrapper for promises
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.