[英]Does a function really need to return promise for async-await to work?
我的疑問是,我可以寫一個普通的 function 並等待調用那個 function 嗎?我會用一個例子更清楚地解釋它,考慮這個 function
let splitStringAndConvertToInt=(data)=>{
let splitData=data.split("-")
let bothValues={
first:parseInt(splitData[0]),
last:parseInt(splitData[1])
}
return bothValues
}
上面是我要打的function,下面是總機function
let allocateRoomAccordingToNeeds=async (request)=>{
let mode=request.mode
let totRooms=request.totalRooms
let firstYearBtech=await splitStringAndConvertToInt(request.firstYearBtech)
let secondYearBtech=await splitStringAndConvertToInt(request.secondYearBtech)
let thirdYearBtech=await splitStringAndConvertToInt(request.thirdYearBtech)
let fourthYearBtech=await splitStringAndConvertToInt(request.fourthYearBtech)
let firstYearMtech=await splitStringAndConvertToInt(request.firstYearMtech)
let secondYearMtech=await splitStringAndConvertToInt(request.secondYearMtech)
let others=await splitStringAndConvertToInt(request.others)
let tutors=await splitStringAndConvertToInt(request.tutors)
let staff=await splitStringAndConvertToInt(request.staff)
if (mode="Category" )
{
Room.updateMany({Room_number:{ $gte:firstYearBtech.first,$lte:firstYearBtech.last }}, {"$set":{"position": "1 year btech"}}, (err, writeResult) => {if(err){console.log("error in 1 btech"+err)} else{
Room.updateMany({Room_number:{ $gte:secondYearBtech.first,$lte:secondYearBtech.last }}, {"$set":{"position": "2 year btech"}}, (err2, writeResult2) => {if(err2){console.log("error in 2 btech"+err2)} else{
Room.updateMany({Room_number:{ $gte:thirdYearBtech.first,$lte:thirdYearBtech.last }}, {"$set":{"position": "3 year btech"}}, (err3, writeResult3) => {if(err3){console.log("error in 3 btech"+err3)} else{
Room.updateMany({Room_number:{ $gte:fourthYearBtech.first,$lte:fourthYearBtech.last }}, {"$set":{"position": "4 year btech"}}, (err4, writeResult4) => {if(err4){console.log("error in 4 b tech"+err4)} else{
Room.updateMany({Room_number:{ $gte:firstYearMtech.first,$lte:firstYearMtech.last }}, {"$set":{"position": "1 year mtech"}}, (err1m, writeResult1m) => {if(err1m){console.log("error in 1 mtech"+err1m)} else{
Room.updateMany({Room_number:{ $gte:secondYearMtech.first,$lte:secondYearMtech.last }}, {"$set":{"position": "2 year mtech"}}, (err2m, writeResult2m) => {if(err2m){console.log("error in 2 mtech"+err2m)} else{
Room.updateMany({Room_number:{ $gte:others.first,$lte:others.last }}, {"$set":{"position": "others"}}, (errO, writeResultO) =>{if(errO){console.log("error in others"+errO)} else{
Room.updateMany({Room_number:{ $gte:tutors.first,$lte:tutors.last }}, {"$set":{"position": "tutors"}}, (errT, writeResultT) => {if(errT){console.log("error in tutor"+errT)} else{
Room.updateMany({Room_number:{ $gte:staff.first,$lte:staff.last }}, {"$set":{"position": "staff"}}, (errS, writeResultS) => {
if(errS){
console.log("error in staff"+errS)
}
else{
console.log("Success allocation of Room")
res.send("Succes allocation of room")
}})
}})
}})
}})
}})
}})
}})
}})
}});
}
else if(mode="Custom" ){
Room.updateMany({"$set":{"position": "Custom"}}, (error, resultUp) => {
if(error){
console.log("error in Custom mode"+error)}
else{
console.log("successful upload of custom mode")
}})
}
}
下面是所有這些的根代碼
router.post('/authentication/changeallocationmode', function(req, res, next){
//add code to assign same years to the given list if they are somewhere else and also add code to warn that given limit is not enough
let vacate=req.body.vacate
if(vacate==true){
Room.updateMany({$set:{Roommates:["Null","Null","Null"],Occupied:false,Allocated:false}})
.then(()=>{allocateRoomAccordingToNeeds(req.body)})
}
else {allocateRoomAccordingToNeeds(req.body)} /*this if-else is done to make it synchronous
so checking of vacate will be done and only after that other process will be done*/
});
正如您在調用splitToStringAndConvertToInt()之后看到的那樣,有一個 Room.updateMany() 查詢,因此需要在查詢之前調用 await 以進行正確的同步,因為 node.js 是異步的。 所以我需要知道的是我可以這樣調用 await 而不返回 promise 嗎?
是的,function 應該返回 promises,或者 function 本身使用 async-await,那么你可以為它使用 await,否則 await 將沒有意義
但是您的 function 不需要是 promise 或 async-await 因為它不需要時間來完成
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.