await is only valid in async function - eval in async

前端 未结 4 1870
北海茫月
北海茫月 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:51

    foo() should not necessarily be async, as that has no effect on the execution context of eval. Instead, a possible solution is to wrap your ctxScript in a self-executing async function, like so: eval("(async () => {" + ctxScript + "})()")

提交回复
热议问题