await is only valid in async function - eval in async

Related searches

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();

the following throws an error: await is only valid in async function

let ctxScript = 'await foo1(); await foo2();';
async function foo()
  eval( ctxScript );

How could I handle this? My foo() should be async as it is Puppetteer controller function

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 + "})()")

await is only valid in async function - eval in async, 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� Because if you want to use await, you must do it in an async function, which means that you must await the response of that function in the function calling it - again, it means that ALL of your functions will need to become async. To me this means that await async is not ready for use.

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

SyntaxError: await is only valid in async function � Issue #700 , Right from the code from openpgp docs: var privKeyObj = openpgp.key. readArmored(privkey).keys[0]; await privKeyObj.decrypt(passphrase);� You don't have a callback, and you can use await with the file and other asynchronous operations; The main async function's returned promise will only resolve when all await-tasks have been performed.

If you want to dynamically call some async code in some larger function, then you can supply a callback that does this for you. This way you can call your function with different extra functionality by giving it different callback functions to execute:

// some sample async functions
var resolveAfter2Seconds = function() {
  console.log("starting slow promise -> ");
  return new Promise(resolve => {
    setTimeout(function() {
      console.log("<- slow promise is done");
    }, 2000);

var resolveAfter1Second = function() {
  console.log("starting fast promise ->");
  return new Promise(resolve => {
    setTimeout(function() {
      console.log("<- fast promise is done");
    }, 1000);

//a function that accepts a callback and would await its execution
async function foo(callback) {
  console.log("-- some code --");
  await callback();
  console.log("-- some more code --");

//calling with an async function that combines any code you want to execute
foo(async () => { 
  await resolveAfter2Seconds();
  await resolveAfter1Second();

"await is only valid in async function" even with top-level await , Change this: signed => {. To this: function(signed) {. And then to this: async function(signed) {. The middle phase is just an intermediate step in order clarify the� Using await essentially lets you pause execution of a function when your expression returns a promise. The catch is that the calling function needs to be async. You should be able to achieve this with: bot.on('message', async (msg) => { // your function body using await }

SyntaxError: await is only valid in async functions and async , uncaught syntaxerror: await is only valid in async function sweetalert2 bcrypt syntaxerror I want to eval() some lines of code inside of async function. While the� So, async ensures that the function returns a promise, and wraps non-promises in it. Simple enough, right? But not only that. There’s another keyword, await, that works only inside async functions, and it’s pretty cool. Await. The syntax:

await is only valid in async function, Uncaught SyntaxError: await is only valid in async function � SyntaxError: /learn ES6: Use the Spread Operator to Evaluate Arrays In-Place� The preceding method has the async modifier in its signature. That signals to the compiler that this method contains an await statement; it contains asynchronous operations. This method represents the task that toasts the bread, then adds butter and jam. This method returns a Task<TResult> that represents the composition of those three

node js error await is only valid for async functions Code Example, An async function can contain an await expression that pauses the execution of the async function and waits for the passed Promise 's resolution, and then resumes the async function's execution and returns the resolved value. Remember, the await keyword is only valid inside async functions. Let us jump into code to understand this better.

  • Take a step back - why do you want to eval the code? If you give us the real problem, perhaps it would turn out there is a different solution
  • I want to execute different actions due to different conditions. My async foo() is big, but only the small pieces of code that I want execute by eval() are different.
  • You can already execute different functions without needing eval - if for example, or calling a whole different function, polymorphism, setting up lookup tables with functionality and so on.
  • Ok, my X problem is that I want to use some different code inside of function, that should be async, what's the best Y to do it? Move all the async() code into includes, like said in answer #1?
  • Why do we always have to have someone saying to not use eval when asking about eval? Just answer the question, there are legitimate use cases for it.
  • My foo() should be async as it is Puppetteer controller function.
  • My point was that whether foo() is async or not has no bearing whether the code inside the eval is in an async execution context or not. Hence my answer.
  • So it has, await is not working inside of eval(), inside of async function.
  • From the comments above, perhaps using eval here is not the best solution to your problem, as it has massive downsides (slower, different execution context, the risk for code injection).
  • I see, eval() there right now only as a quick patch, seeing and understanding and accepting all the risks. But this is my first time with node.js and async so I don't know better ways yet.
  • Sleeping is never really a good idea in code hey. If your eval script ever fails, then it will sleep forever until you restart the system. If you really want to use sleep, maybe wrap everything in a try catch, so that if the code breaks, your sleep will stop. Something like try {eval(ctxScript)} catch () {is_script_ended = true}
  • TY very much for the great example! Now imagine, that I have some objects in the definition of async function foo(), that created after ---some code--- and before callback, and have thousands of different double-awaits (now stored into files), when secondary calling this function foo(), that want to use these objects.
  • ths doesn't explain how to get an expression value from the async, only to call a function later
  • @wowow what's the problem? const result = await callback() will get the value. I didn't think it needs to be stated explicitly, since there is no change in how you'd do that. I was focusing on showing how to avoid eval.