I’ve spent almost no time paying attention to NodeJS, been focused on changes to Ionic. But Node introduced something pretty cool in version 8. So it’s old news, but my guess is that it will still be new news to some people here.
There’s now a promisify
function in Node. So instead of putting FileReader inside new Promise
, to get away from callback hell, you can use Node’s fs library like this:
import { promisify } from 'util';
import { readFile } from 'fs';
readFilePromise(filename: string): Promise<Buffer> {
return promisify(readFile)(filename);
}
Buffer is a Node interface that is basically an arraylike of raw data, that can be translated to string, JSON, etc.