承诺 prompt.js

Promisify prompt.js

最初在 GitHub

上提问

我正在努力承诺 prompt.js。 谁能说我做错了什么?

var prompt = require('prompt');
var Promise = require("bluebird");
Promise.promisifyAll(prompt);
prompt.start().then(function() {
    console.log("test");
    return true;
});
prompt.get(['message'], function(err, result) {
    if (err) {
        return onErr(err);
    }
    console.log('Write a push Message repositoryName:');
    console.log('  Message: ' + result.message);
    return result;
}).then(function(result) {
    console.log("hello");
    return result;
});

当您使用 promisifyAll 通过 bluebird 承诺对象时 - 默认添加 Async 后缀。所以不要调用 get 而是调用 getAsync:

prompt.start(); // start is synchronous, no need to `then` it

prompt.getAsync(["message"]).then(function(response) { // note the suffix added
    console.log("got message", response.message);
    // work with message here, can use promise aggregation/chaining and use like
    // any other promise method
});

引用文档:

The promisified method name will be the original method name suffixed with "Async". Any class properties of the object (which is the case for the main export of many modules) are also promisified, both static and instance methods. Class property is a property with a function value that has a non-empty .prototype object. Returns the input object.

(强调我的)