【发布时间】:2016-09-12 13:52:38
【问题描述】:
const dbConnection = require("../dbConnection");
var task = function () {
var response = "";
dbConnection.then(function () {
//do something here
response = "some value";
})
.catch(function () {
response = new Error("could not connect to DB");
});
//I can't return response here because the promise is not yet resolved/rejected.
}
我正在使用其他人编写的节点模块。它返回一个承诺。我想返回一个字符串或new Error(),具体取决于模块返回的 Promise 对象是否已解析。我该怎么做?
我也无法在 finally() 回调中返回,因为 return 将应用于回调函数而不是我的 task 函数。
【问题讨论】:
-
为什么不能按原样使用模块?
标签: javascript node.js asynchronous promise