简体   繁体   中英

Wait for async function to end

I have the following function that works fine, except I need to wait until it finishes to execute the next statement:

zohoAuth.zoho_oAuth = function () {
    // return new Promise((resolve, reject) => {

        zohoAuth.state = utils.uuid();

        const url = zohoAuth.authorizationURL();

        zohoAuth.popUp(url);
        getAuthCodeFromCatalyst();
        //setTimeout(getAuthCodeFromCatalyst,1000);
        function getAuthCodeFromCatalyst() {
            return new Promise(function (resolve, reject) {
                (async function waitForFoo() {
                    const gotAuthState = await zohoAuth.getUserDataFromStorageState(zohoAuth.state)
                    await gotAuthState;
                    if (gotAuthState) return resolve();
                    setTimeout(waitForFoo, 1000);
                })();
            });
        }


        console.log("bottom of zoho auth")
        return true;
    // });
}

I call the function with this:

zohoAuth.zoho_oAuth();
console.log("done waiting");

How do i wait for this to finish?

You're making this harder on yourself. Make sure to avoid the explicit promise constructor anti-pattern -

zohoAuth.zoho_oAuth = function () {
  zohoAuth.state = utils.uuid();
  const url = zohoAuth.authorizationURL();
  zohoAuth.popUp(url);
  return zohoAuth.getUserDataFromStorageState(zohoAuth.state);
}

You can access the result by attaching a .then handler to the result of your function call -

zohoAuth.zoho_oAuth()
  .then(authState => console.log("bottom of auth state", authState))
  .catch(console.error)

If you want to use async and await , go ahead. If an error occurs, don't catch it. Instead allow it to bubble up and be handled by the caller -

async function doAuth (...) {
  const authState = await zohoAuth.zoho_oAuth()
  console.log("received auth state", authState)
  return "done" // or whatever
})

doAuth().then(console.log, console.error)

You should consider awaiting on the promise. Below snippet shows the difference of using await -

 const asyncFunction = function() { return new Promise(function(resolve, reject) { setTimeout(() => { console.log('inside promise'); resolve(); }, 100); }); } function callWithoutAwait() { asyncFunction(); console.log('after without await function'); } callWithoutAwait(); async function callWithAwait() { await asyncFunction(); console.log('after with await function'); } callWithAwait();

I was able to accomplish what I needed below is the code. Thanks for the help!

zohoAuth.zoho_oAuth = function() {
    zohoAuth.state = utils.uuid();
    const url = zohoAuth.authorizationURL();
    zohoAuth.popUp(url);
    
    return new Promise(function (resolve, reject) {
        (async function waitForFoo() {
            const gotAuthState = await zohoAuth.getUserDataFromStorageState(zohoAuth.state)
            await gotAuthState;
            if (gotAuthState) return resolve();
            setTimeout(waitForFoo, 1000);
        })();
    });
}

And this is the call:

zohoAuth.zoho_oAuth()
  .then(authState => console.log("bottom of auth state", authState))
  .catch(console.error)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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