Bluebird promisifyAll 在普通情况下不起作用
Bluebird promisifyAll doesn't work in a vanilla case
我正在尝试 promisify 一个 Pusher.js 函数,但我有点困惑为什么最后一种技术有效而前两种技术无效,因为前两种技术似乎遵循文档示例。
我得到的错误是:未定义 triggerAsync。
样本
var Pusher = require('pusher');
var pusher = new Pusher(params);
Promise.promisifyAll(Pusher); //this doesn't work for some reason
var triggerAsync = Promise.promisify(pusher.trigger); //this also doesn't work
Promise.promisifyAll(Object.getPrototypeOf(pusher)); //this works
...since the first two seem to be following the documentation example.
最后一个也是!如果我们看一下 documentation for Promise#promisifyAll
over at bluebirdjs.com(在文档的底部),您会发现这个,它简短地解释了为什么您需要采用这种行为:
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:
var ParanoidLib = require("...");
var throwAwayInstance = ParanoidLib.createInstance();
Promise.promisifyAll(Object.getPrototypeOf(throwAwayInstance));
// Like before, from this point on, all new instances + even the throwAwayInstance suddenly support promises
因此,虽然它没有给出我们为什么需要使用 Pusher 执行此操作的明确答案,但我们至少可以确定这是因为它没有 "[make] 它的类 以一种或另一种方式可用。您也可以放心,您不一定做错了(除非万不得已,否则不应使用此方法)。
我正在尝试 promisify 一个 Pusher.js 函数,但我有点困惑为什么最后一种技术有效而前两种技术无效,因为前两种技术似乎遵循文档示例。
我得到的错误是:未定义 triggerAsync。
样本
var Pusher = require('pusher');
var pusher = new Pusher(params);
Promise.promisifyAll(Pusher); //this doesn't work for some reason
var triggerAsync = Promise.promisify(pusher.trigger); //this also doesn't work
Promise.promisifyAll(Object.getPrototypeOf(pusher)); //this works
...since the first two seem to be following the documentation example.
最后一个也是!如果我们看一下 documentation for Promise#promisifyAll
over at bluebirdjs.com(在文档的底部),您会发现这个,它简短地解释了为什么您需要采用这种行为:
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:
var ParanoidLib = require("...");
var throwAwayInstance = ParanoidLib.createInstance();
Promise.promisifyAll(Object.getPrototypeOf(throwAwayInstance));
// Like before, from this point on, all new instances + even the throwAwayInstance suddenly support promises
因此,虽然它没有给出我们为什么需要使用 Pusher 执行此操作的明确答案,但我们至少可以确定这是因为它没有 "[make] 它的类 以一种或另一种方式可用。您也可以放心,您不一定做错了(除非万不得已,否则不应使用此方法)。