Skip to content

Instantly share code, notes, and snippets.

@nvandoorn
Last active July 9, 2018 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nvandoorn/8b25fd64048297e741fb6f53116728fe to your computer and use it in GitHub Desktop.
Save nvandoorn/8b25fd64048297e741fb6f53116728fe to your computer and use it in GitHub Desktop.
const fs = require('fs')
function readFilePromise(path: string, flags: string): Promise<string> {
return new Promise((resolve, reject) => {
fs.readFile(path, flags, (err, data) => {
// if the error exists, reject the promise
// and pass the error, otherwise resolve
// with our data
err ? reject(err) : resolve(data)
})
})
}
(async function() {
try {
const data: string = await readFilePromise('/helloworld', 'r')
console.log(data)
}
catch (err) {
console.error('Oh no')
console.error(err)
}
// or
readFilePromise('/helloworld', 'r')
.then((data: string) => console.log(data))
.catch(err => console.error(err))
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment