async问题,JS Promise无法返回结果,但可以使用console.log

Issue with async, JS Promise not able to return a result, but works with console.log

本文关键字:可以使 console log 结果 JS 问题 Promise async 返回      更新时间:2023-09-26

目前,当我在运行"node jkl.js"时,在"exports.findOneProblem"中使用console.log(result)时,它可以工作。我可以看到结果。然而,当我使用return而不是console.log()时,我得到的只是控制台中的Promise{pending}。请填补空白。。。。学习如何履行承诺,谢谢。

 //asd.js
    exports.findOneProblem = function(problemId) {
          return RClient.authenticate(options).then(function (client) {
            const Problem = client.Problem;
            return Problem.findOne(problemId)
          }).then(function(result){
              return result
          });
        };

第二个文件:jkl.js

var okay = require('./asd');
var moneymoney = okay.findOneProblem(263)
console.log(moneymoney)

var honeyhoney = moneymoney.then(function(result){
  return result
})
console.log(honeyhoney)

当您收到Promise时,这意味着您将在"稍后"获得一个值,即在所有同步代码完成运行之后。访问Promise提供的值的方法是使用.then函数。

moneymoney.then(function(result) {
  console.log(result);
  // Add your code for using the result of `okay.findOneProblem(263)` here
});