0

wait for a function to complete in JavaScript

In the below code, the problem is that console.log() is executed while the uploading is still in process. I want it to wait until upload is completed. function upload(){ //firebase functions to upload file } function submit(){ upload(); console.log("completed"); }

3rd Feb 2020, 4:21 PM
harshit
harshit - avatar
12 Answers
+ 2
if i remember correctly firebase function like set return a Promise so you can easily use async await function upload(){ return firebase.database().ref().set(); } async function submit(){ await upload(); console.log('done'); }
3rd Feb 2020, 4:34 PM
Taste
Taste - avatar
+ 1
if put() return a promise, you can simple return them. return task; if not, this is what i come up with, assuming function complete() are called after the upload are finished. https://code.sololearn.com/WFpO45xJWld7/?ref=app but you can see what i'm doing. wrap them in Promise, then call resolve() when its done or call reject() if error occur.
3rd Feb 2020, 7:56 PM
Taste
Taste - avatar
0
Taste it is giving error Uncaught (in promise) Reference.set failed:Was called with 0 arguments. Expects at least 1.
3rd Feb 2020, 5:22 PM
harshit
harshit - avatar
0
its an error from the send data function. may i see the upload code ? you can remove the auth info if there's any
3rd Feb 2020, 5:26 PM
Taste
Taste - avatar
0
Sure Taste Ignore some errors which might be because I had removed some lines of codes. But you can still get the logic. https://code.sololearn.com/W7p3UEuIJ1E5/?ref=app
3rd Feb 2020, 7:01 PM
harshit
harshit - avatar
3rd Feb 2020, 7:50 PM
Taste
Taste - avatar
0
Taste thanks. Also is my way of doing captcha verification correct ?
3rd Feb 2020, 9:32 PM
harshit
harshit - avatar
0
you can follow the instruction here https://developers.google.com/recaptcha/docs/v3
3rd Feb 2020, 10:03 PM
Taste
Taste - avatar
0
Taste I have used recaptcha v2. The link is about v3. In the js file I have used following code to verify recaptcha. But on internet it is given that this is only doing client side verification and not verifying using Google's server. var response = grecaptcha.getResponse(); if(response.length == 0) { //reCaptcha not verified alert("please verify you are human !"); return false; } else{ // execute code }
4th Feb 2020, 6:41 AM
harshit
harshit - avatar
0
btw getResponse only return a token, you'll need to do an api request with the token you're recieved the response will always be recieved even if the captcha failed so using response.length == 0 will always false (except google is down or something). you can use .success from the response to see if it failed or not. https://developers.google.com/recaptcha/docs/verify
4th Feb 2020, 6:55 AM
Taste
Taste - avatar
0
Taste the code is working in my case and when I am not verifying the recaptcha it is giving the alert message. Can you send me the code/example of the server side verification as in google developer's document it is given very vaguely and I am a beginner. Thanks !
4th Feb 2020, 7:54 AM
harshit
harshit - avatar
0
depend on yoir backend languange the implementation can be different. for example in node using request package request.post({url:"https://www.google.com/recaptcha/api/siteverify",form:{secret: secret_key,response:req.body.token,remoteip:req.connection.remoteAddress}}, (error, response,body)=>{ let result= JSON.parse(body); if(result.status){ //verified and process }else{ //failed } }); it may be possible using ajax in browser, but it'll expose your secret key which is never good
4th Feb 2020, 8:21 AM
Taste
Taste - avatar