Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using bluebird with undefined success callback function

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?

like image 921
Teoman shipahi Avatar asked Feb 01 '16 19:02

Teoman shipahi


People also ask

What are promise based APIs in Bluebird?

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.

How to promisify in Bluebird?

// 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:

How to use Bluebird from within a node application?

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.

What is Bluebird JS?

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.


1 Answers

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.

like image 145
Retsam Avatar answered Oct 19 '22 13:10

Retsam