Skip to content

Instantly share code, notes, and snippets.

@deecewan
Last active October 10, 2017 13:35
Show Gist options
  • Save deecewan/62d42a6a9d8951cb3d8a5de7feddfa99 to your computer and use it in GitHub Desktop.
Save deecewan/62d42a6a9d8951cb3d8a5de7feddfa99 to your computer and use it in GitHub Desktop.
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;
});
> 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