Search code examples
node.jses6-promise

Promises in Node.js core


I hear that Promises are available for Node core APIs. For example with fs, how can we use promises, do we just omit the callback?

fs.readFile(file).then(v => console.log(v)); 

or how do we use?

at the moment for Node.js versions older than 10, I am guessing, it's behind a flag? Maybe: node --promises?


Solution

  • This experimental feature was added in node v10.0.0. You'll need to require fs/promises instead of fs