Using filesystem in node.js with async / await
Starting with node 8.0.0, you can use this:
const fs = require('fs');const util = require('util');const readdir = util.promisify(fs.readdir);async function myF() { let names; try { names = await readdir('path/to/dir'); } catch (err) { console.log(err); } if (names === undefined) { console.log('undefined'); } else { console.log('First Name', names[0]); }}myF();
See https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original
Native support for async await fs functions since Node 11
Since Node.JS 11.0.0 (stable), and version 10.0.0 (experimental), you have access to file system methods that are already promisify'd and you can use them with try catch
exception handling rather than checking if the callback's returned value contains an error.
The API is very clean and elegant! Simply use .promises
member of fs
object:
import fs from 'fs';const fsPromises = fs.promises;async function listDir() { try { return fsPromises.readdir('path/to/dir'); } catch (err) { console.error('Error occured while reading directory!', err); }}listDir();
Node.js 8.0.0
Native async / await
Promisify
From this version, you can use native Node.js function from util library.
const fs = require('fs')const { promisify } = require('util')const readFileAsync = promisify(fs.readFile)const writeFileAsync = promisify(fs.writeFile)const run = async () => { const res = await readFileAsync('./data.json') console.log(res)}run()
Promise Wrapping
const fs = require('fs')const readFile = (path, opts = 'utf8') => new Promise((resolve, reject) => { fs.readFile(path, opts, (err, data) => { if (err) reject(err) else resolve(data) }) })const writeFile = (path, data, opts = 'utf8') => new Promise((resolve, reject) => { fs.writeFile(path, data, opts, (err) => { if (err) reject(err) else resolve() }) })module.exports = { readFile, writeFile}...// in some file, with imported functions above// in async blockconst run = async () => { const res = await readFile('./data.json') console.log(res)}run()
Advice
Always use try..catch
for await blocks, if you don't want to rethrow exception upper.