How do you properly promisify request?

The following should work: var request = Promise.promisify(require(“request”)); Promise.promisifyAll(request); Note that this means that request is not a free function since promisification works with prototype methods since the this isn’t known in advance. It will only work in newer versions of bluebird. Repeat it when you need to when forking the request object for cookies. … Read more

How does Bluebird’s util.toFastProperties function make an object’s properties “fast”?

2017 update: First, for readers coming today – here is a version that works with Node 7 (4+): function enforceFastProperties(o) { function Sub() {} Sub.prototype = o; var receiver = new Sub(); // create an instance function ic() { return typeof receiver.foo; } // perform access ic(); ic(); return o; eval(“o” + o); // ensure … Read more

How to promisify Node’s child_process.exec and child_process.execFile functions with Bluebird?

I would recommend using standard JS promises built into the language over an additional library dependency like Bluebird. If you’re using Node 10+, the Node.js docs recommend using util.promisify which returns a Promise<{ stdout, stderr }> object. See an example below: const util = require(‘util’); const exec = util.promisify(require(‘child_process’).exec); async function lsExample() { try { … Read more

While loop using bluebird promises

cast can be translated to resolve. defer should indeed not be used. You’d create your loop only by chaining and nesting then invocations onto an initial Promise.resolve(undefined). function promiseWhile(predicate, action, value) { return Promise.resolve(value).then(predicate).then(function(condition) { if (condition) return promiseWhile(predicate, action, action()); }); } Here, both predicate and action may return promises. For similar implementations also … Read more