await is only valid in async function - eval in async

前端 未结 4 1887
北海茫月
北海茫月 2020-12-17 23:59

I want to eval() some lines of code inside of async function. While the following code is ok,

async function foo()
{
  await foo1();
  await foo2();
}
         


        
4条回答
  •  粉色の甜心
    2020-12-18 00:52

    Ended up using Ermir`s answer:

    let ctxScript = '(async () => {await foo1();await foo2();is_script_ended = true; })();';
    
    async function foo()
    {
      // a lot of code
      is_script_ended = false;
      eval( ctxScript );
      while(!is_script_ended){ await sleep(1000); }
      // a lot of code
    }
    

提交回复
热议问题