3 ms·
Notejs should get promise versions of its current callback-based APIs: const fs = require("fs"); fs.writeFile("Hello, World", "helloworld.txt", (error)
by p5k 9y ago
Notejs should get promise versions of its current callback-based APIs:
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));
- __s 9y agohttps://nodejs.org/api/util.html#util_util_promisify_original https://nodejs.org/api/util.html#util_util_promisify_origina...
- STRML 9y agoThis was tried many years ago. At that time, Promises/A+ was not finalized, and the community could not agree on which Promise specification was best, or even if one was needed at all. 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.
- deleted 9y ago[deleted]
- flavio81 9y agoIt really should be: 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.