2016-05-12 18 views
0

当我在运行“node jkl.js”的同时在我的“exports.findOneProblem”中使用console.log(result)时,它目前工作正常。我能看到结果。但是,当我使用return而不是console.log()时,我得到的只是控制台中的Promise {pending}。请填写空白....学习如何与承诺一起工作,谢谢。异步问题,JS Promise无法返回结果,但与console.log一起工作

//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) 

回答

1

当您收到一个承诺,这意味着你要经过所有的同步代码来获得一个值“后”,即完成运行时。访问Promise提供的值的方法是使用.then函数。

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

哦,我的!谢谢。 –