Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

functional try-catch wrapper for promises

License

NotificationsYou must be signed in to change notification settings

coderaiser/try-to-catch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Functionaltry-catch wrapper forpromises.

Install

npm i try-to-catch

API

tryToCatch(fn, [...args])

Wrap function to avoidtry-catch block, resolves[error, result];

Example

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

Related

License

MIT


[8]ページ先頭

©2009-2025 Movatter.jp