by p5k · 9y ago · 0 comments
const fs = require("fs");
fs.writeFile("Hello, World", "helloworld.txt", (error)=>{
if(error) throw error;
console.log("done!");
});
Should be: const fs = require("fs");
fs.writeFilePromise("Hello, World", "helloworld.txt").then(()=>console.log("done!"),error=>console.error(error)); try{
x = await fs.writeFilePromise("Hello, World", "helloworld.txt")
console.log("done!")
}
catch(error)
{
console.error(error) // or console.log
}
This takes advantage of promises fully.Callbacks are lightest-weight re: CPU & memory overhead, so it was decided that core APIs should implement that, and developers can override using promisify (via e.g. Bluebird or the new `util.promisify()`) as they need. But putting that kind of assumption in core could lead to significant pain.