繁体   English   中英

function 真的需要返回 promise 才能让异步等待工作吗?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM