【问题标题】:Omitting arguments causes promisified functions to fail for ES2015 classes省略参数会导致 ES2015 类的承诺函数失败
【发布时间】:2017-08-23 17:26:04
【问题描述】:

当省略参数时,我无法让 Bluebird librarypromisifyAll 函数与 ES2015 类的对象实例方法一起使用。我正在使用节点 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() 会尝试调用第二个参数作为回调,但回调不存在。

标签: node.js ecmascript-6 promise bluebird


【解决方案1】:

是的,这是正确的行为。承诺被映射到原始函数。所以fetchBone() 在第一个调用中接收两个参数,在第二个示例中接收一个。

这意味着在第二个示例中,当fetchBone() 被调用时,callback 是未定义的,bone 本身就是 Promisification 创建的用于处理 Promise 本身的回调函数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-09
    • 2012-03-21
    • 2020-02-29
    • 2023-04-03
    • 1970-01-01
    • 2022-09-24
    • 1970-01-01
    相关资源
    最近更新 更多