Tutorial by Examples

Let's say you have a library that returns callbacks, for example the fs module in NodeJS: const fs = require("fs"); fs.readFile("/foo.txt", (err, data) => { if(err) throw err; console.log(data); }); We want to convert it to a promise returning API, with bluebird - ...
You can convert a single function with a callback argument to a Promise-returning version with Promise.promisify, so this: const fs = require("fs"); fs.readFile("foo.txt", (err, data) => { if(err) throw err; console.log(data); }); becomes: const promisify = requ...
In order to convert any callback API to promises assuming the promisify and promisifyAll version doesn't fit - you can use the promise constructor. Creating promises generally means specifying when they settle - that means when they move to the fulfilled (completed) or rejected (errored) phase to i...

Page 1 of 1