我正在尝试制作一个返回xkcd密码的函数,如下所示:bat-ship-eight-loophole。该库可以在这里找到:https://github.com/fardog/node-xkcd-password

这是我的代码:

var xkcdPassword = require('xkcd-password')
var pw = new xkcdPassword()

var options = {
  numWords: 4,
  minLength: 5,
  maxLength: 8
}

// or, with promises
function generateCode() {
    pw.generate(options).then(function (result) {
        return "hello"
    })
}

console.log(generateCode())


我认为我的问题与图书馆无关

最佳答案

承诺是表示可能尚未完成的异步操作的一种方式。为了获得输出,您必须使函数返回Promise,并使用then添加接收输出的回调函数。

function generateCode() {
    return pw.generate(options);
}

generateCode().then(function(code) {
    console.log(code);
});

// pw.generate(options).then(console.log); // also works in this case

09-25 17:54