使用bluebird与未定义的成功回调函数

Teo*_*ahi 6 javascript memcached node.js promise bluebird

我在memcached上使用bluebird库.

memcached.set('foo', 'bar', 10, function (err) { /* stuff */ });
Run Code Online (Sandbox Code Playgroud)

这个函数不会在第二个参数中调用成功回调,所以看起来像.then(res)函数没有被调用.

 Promise.promisifyAll(memcached);
 memcached.setAsync(hashedCacheKey, obj).then(function (res) {
            resolve(res);
        }).catch(function (err) {
            reject(err, null);
        });
Run Code Online (Sandbox Code Playgroud)

我有什么方法可以处理无谓的成功事件吗?

Ret*_*sam 4

这里的主要问题是您没有为 提供超时参数memcached.setAsync,但它是 的强制参数memcached.set。这两行是等价的:

memcached.set("foo", "bar", () => { /* this is never called */ });
memcached.setAsync("foo", "bar").then(() => { /* this is never called, either */ })
Run Code Online (Sandbox Code Playgroud)

添加超时参数,您的代码应该按预期工作。