Last active
July 9, 2018 18:53
-
-
Save nvandoorn/8b25fd64048297e741fb6f53116728fe to your computer and use it in GitHub Desktop.
This file contains 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
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