省略参数会导致 ES2015 的 promisified 函数失败 类
Omitting arguments causes promisified functions to fail for ES2015 classes
当省略参数时,我无法让 Bluebird library 的 promisifyAll
函数与 ES2015 class 的对象实例方法一起使用。我正在使用节点 7.8.0。这是一些示例代码:
// dog.js
class Dog {
constructor(opts) {
this.name = opts.name || 'Fido';
}
fetchBone(bone, callback) {
console.log('fetching the bone');
callback(null, 'got it');
}
}
exports = module.exports = Dog;
假设骨骼是 fetchBone
的可选参数。当我通过它时,一切都很好。
> var Dog = require('./dog');
> Promise = require('bluebird');
> Promise.promisifyAll(Dog); // omitting this line doesn't help
> Promise.promisifyAll(Dog.prototype);
> var puppy = new Dog();
> puppy.fetchBoneAsync('juicy bone')
.then(result => console.log('from promise:', result))
.catch(err => console.error('failure:', err));
fetching the bone
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
> from promise: got it
不传骨头就失败了
> puppy.fetchBoneAsync()
.then(result => console.log('from promise:', result))
.catch(err => console.error('failure:', err));
fetching the bone
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
> failure: TypeError: callback is not a function
at Dog.fetchBone (dog.js:8:5)
at Dog.tryCatcher (node_modules/bluebird/js/release/util.js:16:23)
at Dog.ret [as fetchBoneAsync] (eval at makeNodePromisifiedEval (node_modules/bluebird/js/release/promisify.js:184:12), <anonymous>:14:23)
at repl:1:7
at ContextifyScript.Script.runInThisContext (vm.js:23:33)
at REPLServer.defaultEval (repl.js:339:29)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.onLine (repl.js:536:10)
at emitOne (events.js:101:20)
at REPLServer.emit (events.js:191:7)
at REPLServer.Interface._onLine (readline.js:241:10)
at REPLServer.Interface._line (readline.js:590:8)
at REPLServer.Interface._ttyWrite (readline.js:869:14)
at REPLServer.self._ttyWrite (repl.js:609:7)
at ReadStream.onkeypress (readline.js:120:10)
奇怪的是,如果我为骨骼传入 undefined
它会起作用。
> puppy.fetchBoneAsync(undefined)
.then(result => console.log('from promise:', result))
.catch(err => console.error('failure:', err));
fetching the bone
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
> from promise: got it
有人知道这是怎么回事吗?
是的,这是正确的行为。承诺被映射到原始功能。所以 fetchBone()
在第一个调用中接收两个参数,在第二个示例中接收一个。
这意味着在第二个例子中,当 fetchBone()
被调用时 callback
是未定义的, bone
本身是由 promisification 创建的回调函数来处理 promise 本身。
当省略参数时,我无法让 Bluebird library 的 promisifyAll
函数与 ES2015 class 的对象实例方法一起使用。我正在使用节点 7.8.0。这是一些示例代码:
// dog.js
class Dog {
constructor(opts) {
this.name = opts.name || 'Fido';
}
fetchBone(bone, callback) {
console.log('fetching the bone');
callback(null, 'got it');
}
}
exports = module.exports = Dog;
假设骨骼是 fetchBone
的可选参数。当我通过它时,一切都很好。
> var Dog = require('./dog');
> Promise = require('bluebird');
> Promise.promisifyAll(Dog); // omitting this line doesn't help
> Promise.promisifyAll(Dog.prototype);
> var puppy = new Dog();
> puppy.fetchBoneAsync('juicy bone')
.then(result => console.log('from promise:', result))
.catch(err => console.error('failure:', err));
fetching the bone
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
> from promise: got it
不传骨头就失败了
> puppy.fetchBoneAsync()
.then(result => console.log('from promise:', result))
.catch(err => console.error('failure:', err));
fetching the bone
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
> failure: TypeError: callback is not a function
at Dog.fetchBone (dog.js:8:5)
at Dog.tryCatcher (node_modules/bluebird/js/release/util.js:16:23)
at Dog.ret [as fetchBoneAsync] (eval at makeNodePromisifiedEval (node_modules/bluebird/js/release/promisify.js:184:12), <anonymous>:14:23)
at repl:1:7
at ContextifyScript.Script.runInThisContext (vm.js:23:33)
at REPLServer.defaultEval (repl.js:339:29)
at bound (domain.js:280:14)
at REPLServer.runBound [as eval] (domain.js:293:12)
at REPLServer.onLine (repl.js:536:10)
at emitOne (events.js:101:20)
at REPLServer.emit (events.js:191:7)
at REPLServer.Interface._onLine (readline.js:241:10)
at REPLServer.Interface._line (readline.js:590:8)
at REPLServer.Interface._ttyWrite (readline.js:869:14)
at REPLServer.self._ttyWrite (repl.js:609:7)
at ReadStream.onkeypress (readline.js:120:10)
奇怪的是,如果我为骨骼传入 undefined
它会起作用。
> puppy.fetchBoneAsync(undefined)
.then(result => console.log('from promise:', result))
.catch(err => console.error('failure:', err));
fetching the bone
Promise {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
> from promise: got it
有人知道这是怎么回事吗?
是的,这是正确的行为。承诺被映射到原始功能。所以 fetchBone()
在第一个调用中接收两个参数,在第二个示例中接收一个。
这意味着在第二个例子中,当 fetchBone()
被调用时 callback
是未定义的, bone
本身是由 promisification 创建的回调函数来处理 promise 本身。