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对象是否已解析。我怎样才能做到这一点?根据是否已解析Promise从函数返回值
我不能finally()
回调中返回或者因为那return
将适用于回调函数不是我task
功能。
为什么你不能原样使用该模块? –