I am using bluebird library over memcached.
memcached.set('foo', 'bar', 10, function (err) { /* stuff */ });
this function does not call success callback in second parameter so seems like .then(res) function does not getting called.
Promise.promisifyAll(memcached);
memcached.setAsync(hashedCacheKey, obj).then(function (res) {
resolve(res);
}).catch(function (err) {
reject(err, null);
});
is there any way for me to handle uncalled success event?
This APIs are what most core modules in Node/io use and bluebird comes with a fast and efficient way to convert them to promise based APIs through the Promise.promisifyand Promise.promisifyAllfunction calls. Promise.promisify- converts a singlecallback taking function into a promise returning function.
// pgvarPromise=require("bluebird");Promise.promisifyAll(require("pg")); In all of the above cases the library made its classes available in one way or another. If this is not the case, you can still promisify by creating a throwaway instance:
To use Bluebird from within a Node application, the Bluebird module is required. To install the Bluebird module, run the below command The next step is to include the bluebird module in your code and promisify the entire MongoDB module.
Bluebird JS is a fully-featured Promise library for JavaScript. The strongest feature of Bluebird is that it allows you to “promisify” other Node modules in order to use them asynchronously. Promisify is a concept applied to callback functions.
The primary issue here is that you're not providing a timeout argument to memcached.setAsync
, but it's a mandatory argument for memcached.set
. These two lines are equivalent:
memcached.set("foo", "bar", () => { /* this is never called */ });
memcached.setAsync("foo", "bar").then(() => { /* this is never called, either */ })
Add a timeout argument and your code should work as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With