在ES6 + babel中使用蓝鸟宣传导入的类(构造函数)

lef*_*ead 6 javascript constructor node.js promise bluebird

假设我创建或拥有node.js库 lib.js

export class C {
    constructor(value, callback) {
        callback(false, `Hello ${value}`);
    }

    task(value, callback) {
        callback(false, "returned " + value);
    }
}
Run Code Online (Sandbox Code Playgroud)

重要的是,类的构造函数需要接受回调,因为它执行数据库连接和文件I/O. 如果我现在导入并使用库回调式,一切都很好(见c1下文).

我真的想宣传我使用它的库,使对象构造更方便(实际上它是一大堆类和方法).

但是,我无法new在一个承诺安全的地方找到适当的方法.

import Promise from 'bluebird';
import * as lib from './lib';


Promise.promisifyAll(lib);


// old style -- works as expected
const c1 = new lib.C("c1", (e, v) => {
    console.log(c1, e, v); 
});


// assuming c1 got initialized, .task() also works
c1.task("t1", console.log);
c1.taskAsync("t2").then(() => console.log("also works"));


// But how to do this properly with promises?
const c2 = new lib.C("c2"); c2.then(console.log); // clearly doesn't work, lack of callback
const c3 = new lib.CAsync("c3"); c3.then(console.log); // "cannot read property apply of undefined"
const c4 = ???
Run Code Online (Sandbox Code Playgroud)

我怎么做到最好?更改库签名不是一个好选择,创建工厂方法似乎也很难看.

Ben*_*aum 6

我对此非常感兴趣,所以我将从它开始:不要在构造函数中执行IO,将io和构造绑定在一起是个坏主意.

也就是说,如果你必须这样做,因为库不在你的控制范围内,并且没有能力以同步的方式构建对象,你可以:

export class C {
    constructor(value, callback) {
        callback(false, `Hello ${value}`);
    }

    task(value, callback) {
        callback(false, "returned " + value);
    }
}
Run Code Online (Sandbox Code Playgroud)

在宣传时:

import Promise from 'bluebird';
import * as lib from './lib';


Promise.promisifyAll(lib);

var old = lib.C; // reference the constructor
lib.C = function(value){ // override it
  o; // object we'll later return, populate in promise constructor
  var p = new Promise(function(resolve, reject){ 
    // the promise constructor is always sync, so the following works
    o = new old(value, function(err, data) {
      if(err) return reject(err);
      resolve(data);   
    });
  }); 
  // THIS IS THE IMPORTANT PART
  o.then = p.then.bind(p); // make the object a thenable, 
  return o
};
Run Code Online (Sandbox Code Playgroud)

这将使你们都使用返回值和promise,promise只会有一个then所以你可能希望Promise.resolve它获得一个"真正的"承诺而不是一个具有属性 promise 的对象.

var o = new lib.C(); // get object
o.then(function(data){
    // access data
});
Run Code Online (Sandbox Code Playgroud)

这可以提取为一种模式:

 function promisifyConstructor(cons){
   return function(...args) => { // new constructor function
     let o;
     let p = new Promise((resolve, reject) => {
         // delegate arguments
        o = new cons(...args, (err, data) => err ? reject(err) : resolve(data));
     });
     o.then = p.then.bind(p);
     return o;
   }
 }
Run Code Online (Sandbox Code Playgroud)