Last active
October 10, 2017 13:35
-
-
Save deecewan/62d42a6a9d8951cb3d8a5de7feddfa99 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fs from 'fs'; | |
class FSError extends Error {} | |
const readFile = (...args) => | |
new Promise((resolve, reject) => { | |
fs.readFile(...args, (err, data) => { | |
if (err) { | |
return reject(new FSError(err)); | |
} | |
return resolve(data); | |
}) | |
}) | |
readFile('./README.md') | |
.then(content => console.log('content')) | |
.catch((err) => { | |
if (err instanceof FSError) { | |
console.log('oops. too bad', err); | |
return; | |
} | |
throw err; | |
}); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> readFile('./README.md').then(content => console.log('content')).catch((err) => { | |
if (err instanceof FSError) { | |
console.log('oops. too bad', err); | |
return; | |
} | |
throw err; | |
}); | |
Promise { | |
<pending>, | |
domain: | |
Domain { | |
domain: null, | |
_events: { error: [Function: debugDomainError] }, | |
_eventsCount: 1, | |
_maxListeners: undefined, | |
members: [] } } | |
> oops. too bad Error: Error: ENOENT: no such file or directory, open './README.md' | |
at ReadFileContext.fs.readFile [as callback] (repl:5:23) | |
at FSReqWrap.readFileAfterOpen [as oncomplete] (fs.js:421:13) | |
> readFile(null).then(content => console.log('content')).catch((err) => { | |
if (err instanceof FSError) { | |
console.log('oops. too bad', err); | |
return; | |
} | |
throw err; | |
}); | |
Promise { | |
<pending>, | |
domain: | |
Domain { | |
domain: null, | |
_events: { error: [Function: debugDomainError] }, | |
_eventsCount: 1, | |
_maxListeners: undefined, | |
members: [] } } | |
> (node:97) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): TypeError: path must be a string or Buffe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment