Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

javascript - Node: get function response synchronously


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

//Using callback

const tempIsEmailExistsInDb = isEmailExistsInDb("[email protected]", function(res) {
        if (res) {//do something
         }
      });
        
           const isEmailExistsInDb = (usrEmail, cb) => {
            let tempCount;
            userModel.countDocuments( {"email": usrEmail}, function (err, count){
                  tempCount = count;
                  cb((tempCount > 0))
              });
             
         };

Also for using async method

     router.get('/' , async (req, res) => {
        const isEmail = await isEmailExistsInDb("[email protected]");
        if (isEmail > 0) {

        }

     })

 const isEmailExistsInDb = async (usrEmail, cb) => {
                
                let tempCount = await userModel.countDocuments( {"email": usrEmail});

                return tempCount;
    
             };

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...