简体   繁体   English

等待异步 function 结束

[英]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:我有以下 function 工作正常,除了我需要等到它完成才能执行下一条语句:

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:我用这个调用 function:

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 -确保避免显式 promise 构造函数反模式-

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 -您可以通过将.then处理程序附加到 function 调用的结果来访问结果 -

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.如果你想使用asyncawait ,提前 go。 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 -您应该考虑在 promise 上awaiting 。下面的代码片段显示了使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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