我怎样才能保证这些功能?

How can I promisfy these functions?

我正在使用 Node.js 和 Bluebird promises 库。

这段代码完全符合我的要求:

/*
 * Try using Bluebird promisify():
 * - "Good" case: this works perfectly.  
 *   ... but it DOESN'T use "promisify()"; it creates a new promise for each function.
 * - SAMPLE OUTPUT:
 *     callABC()...
 *     a():  [ 'a' ]
 *     b():  [ 'a', 'b' ]
 *     c():  [ 'a', 'b', 'c' ]
 *     Done: results: [ 'a', 'b', 'c' ] 
 */
var Promise = require('bluebird');

var a = function (results) {
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      results.push("a");
      console.log("a(): ", results);
      resolve(results);
    }, 15);
  });
}

var b = function (results) {
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      results.push("b");
      console.log("b(): ", results);
      resolve(results);
    }, 5);
  });
}

var c = function (results) {
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      results.push("c");
      console.log("c(): ", results);
      resolve(results);
    }, 10);
  });
}


var callABC = function (results) {
  console.log("callABC()...");
  a(results)
  .then(b)
  .then(c)
  .then(function (results) {
    console.log("Done: results:", results);
  })
  .catch(function (err) {
    console.log("Error:", err);
  });
}

callABC([]);

我理解像这样手动实例化你自己的promise可以考虑"bad":

问:如何 "promisify" 上面的代码片段?

我已经尝试了很多东西; none 其中有效。例如:

/*
 * Try using Bluebird promisify():
 * - Fails: never calls b() or c() 
 */
var Promise = require('bluebird');

var a = Promise.promisify(function (results) {
  setTimeout(function() {
    results.push("a");
    console.log("a(): ", results);
  }, 15);
});

var b = Promise.promisify(function (results) {
  setTimeout(function() {
    results.push("b");
    console.log("b(): ", results);
  }, 5);
});

var c = Promise.promisify(function (results) {
  setTimeout(function() {
    results.push("c");
    console.log("c(): ", results);
  }, 10);
});

var callABC = function (results) {
  console.log("callABC()...");
  a(results)
  .then(b)
  .then(c)
  .then(function (results) {
    console.log("Done: results:", results);
  })
  .catch(function (err) {
    console.log("Error:", err);
  });
}

callABC([]);

问:"promisify" 第一个例子的正确方法是什么?

问:特别是,如果我将自动 Promise.promisify()Promise.promisifyAll() 替换为手动 new Promise(),我该如何 "resolve()" 或 "reject()" 我的回调?我想 "throw" 会调用 .catch(),但是还有其他(更好的?)机制吗?

问:有什么限制吗?例如,函数是否需要有一个回调参数才能成为"promisified"?

答案是:"not all JS functions can be promisified"。具体来说:

http://bluebirdjs.com/docs/api/promise.promisify.html

Promise.promisify)... returns a function that will wrap the given nodeFunction. Instead of taking a callback, the returned function will return a promise whose fate is decided by the callback behavior of the given node function. The node function should conform to node.js convention of accepting a callback as last argument and calling that callback with error as the first argument and success value on the second argument.

所以在上面的例子中,"new Promise"是正确的做法:"Promise.promisify()"是行不通的。