[英]Call a function is JavaScript that calls a promise function and returns the result to the original function
我有兩個功能(控制器中的一個功能和服務中的 oen 功能)。 我想從控制器功能調用服務功能。 調用服務函數后,此函數調用數據庫函數(使用 Sequelize)。
控制器:
exports.getMilestoneById = async (req, res, next) => {
const milestoneId = req.params.milestoneId;
try{
milestoneService.getMilestoneById(milestoneId)
.then(mielstone => {
console.log("Milestone in tehn: " + mielstone)
});
console.log("Milestone: " + milestone);
res.status(200).json({message: 'Milestone fetched', milestone: milestone});
} catch (error){
if (!error.statusCode){
error.statusCode = 500;
}
}
}
服務:
exports.getMilestoneById = async (milestoneId) => {
Milestone.findByPk(milestoneId)
.then(milestone => {
console.log("In Service: " + milestone)
return milestone.get();
})
.catch(err => {
console.log("Error" + err);
});
}
問題:我沒有得到一個里程碑。
好的,讓我們嘗試在這里下訂單。
您的代碼不會等待Promise
響應。
milestoneService.getMilestoneById(milestoneId) // <- this returns a promise
// all stuff below here doesn't wait for the promise to complete and can't see its result
console.log("Milestone: " + milestone);
res.status(200).json({message: 'Milestone fetched', milestone: milestone});
相反,編寫如下內容:
milestoneService.getMilestoneById(milestoneId)
.then(mielstone => {
console.log("Milestone in tehn: " + mielstone);
// I moved all the stuff that needs to wait in here!!
console.log("Milestone: " + milestone);
res.status(200).json({message: 'Milestone fetched', milestone: milestone});
});
這將按預期工作。
你在函數之前添加了async
,我想你可以好好利用它! async / await
通過讓包裝函數等待其結果來幫助您清理 Promise 流,因此編寫如下內容:
const milestone = await milestoneService.getMilestoneById(milestoneId)
console.log("Milestone: " + milestone);
res.status(200).json({message: 'Milestone fetched', milestone: milestone});
也會按預期工作。
... 或不! 您幾乎錯過了代碼中的所有返回。 作為一個經驗法則,盡量讓每個函數總是返回一些東西,特別是如果它是承諾的! 就像:
exports.getMilestoneById = async (milestoneId) => {
const milestone = await Milestone.findByPk(milestoneId);
return milestone.get();
/* I don't suggest handling the error here, because it doesn't bubble well!
/* If there's an error what would this function return?
}
然后
exports.getMilestoneById = async (req, res, next) => {
const milestoneId = req.params.milestoneId;
try {
const milestone = await milestoneService.getMilestoneById(milestoneId)
console.log("Milestone: " + milestone);
// just return this
return res.status(200).json({ message: 'Milestone fetched', milestone });
} catch (error){
if (!error.statusCode){
error.statusCode = 500;
}
// call next with the error!
return next(error);
}
}
當您在此回調中返回結果時:
.then(milestone => {
console.log("In Service: " + milestone)
return milestone.get();
})
您實際上是在設置回調函數的返回值,而不是您的服務函數milestone service.getMilestoneById()
的返回值。 因為你的函數是一個async
函數,你應該使用 Javascript 的 async-await 語法,它只會在 promise 被解決后才獲取值。
exports.getMilestoneById = async (milestoneId) => {
const milestone = await Milestone.findByPk(milestoneId)
.catch(err => {
console.log("Error" + err);
});
console.log("In Service: " + milestone)
return milestone
}
這對於您的控制器功能應該是相同的:
exports.getMilestoneById = async (req, res, next) => {
const milestoneId = req.params.milestoneId;
try{
const milestone = await milestoneService.getMilestoneById(milestoneId)
console.log("Milestone: " + milestone);
res.status(200).json({message: 'Milestone fetched', milestone:
milestone});
} catch (error){
if (!error.statusCode){
error.statusCode = 500;
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.