2017-03-30 2 views
0

인수가 생략되었을 때 Bluebird librarypromisifyAll 함수가 ES2015 클래스의 객체 인스턴스 메소드와 함께 작동하는 데 문제가 있습니다. 노드 7.8.0을 사용하고 있습니다. 여기에 몇 가지 예제 코드는 다음과 같습니다 인수를 생략하면 ES2015 클래스에서 promisified 함수가 실패합니다.

// 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 

아무도 몰라요.

+0

fetchBone()은 가변 인수를 처리하지 않습니다. 첫 번째 인수가 전달되지 않고 작동하도록 코딩하면 약속 된 버전도 작동합니다. 문제는 오직 하나의 인자를 넘겨 줄 때'fetchBone()'이 두 번째 인자를 콜백으로 호출하려고 시도하지만 콜백이 존재하지 않는다는 것입니다. – jfriend00

답변

1

예, 올바른 동작입니다. 약속은 원래 기능에 매핑됩니다. 따라서 fetchBone()은 첫 번째 호출에서 두 개의 인수를 수신하고 두 번째 예에서는 두 개의 인수를 수신합니다.

이 두 번째 예 fetchBone()callback가 호출 될 때 정의임을 의미하고 bone 자체 약속 자체를 처리하는 promisification 의해 생성 콜백 함수이다.

관련 문제