6

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?

Teoman shipahi
  • 47,454
  • 15
  • 134
  • 158
  • @raina77ow it is getting generated after "Promise.promisifyAll(memcached);" I edited my post. – Teoman shipahi Feb 01 '16 at 19:13
  • Sidenote, it looks like you might be unnecessarily wrapping your logic in a `new Promise` constructor. – Retsam Feb 01 '16 at 19:41
  • @Retsam I am returning new promise since calling function is using then/catch signature as well. – Teoman shipahi Feb 01 '16 at 19:50
  • 1
    @Teomanshipahi It sounds like you're using the Promise constructor antipattern. You really should just return `memcached.setAsync`'s promise directly. See [here](http://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it). – Retsam Feb 01 '16 at 19:54
  • @Retsam two answers in one question! Thanks for pointing that out, I changed the way you suggested and much more clear now. Thanks! – Teoman shipahi Feb 01 '16 at 19:59
  • @Teomanshipahi No problem, glad it's working. – Retsam Feb 01 '16 at 20:08

1 Answers1

4

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.

Retsam
  • 30,909
  • 11
  • 68
  • 90