Is there a way to call sleep() without await keyword?

Is there a way to call sleep() without await keyword?

c# call async method without await
configureawait
async without await c#
c# wait for method to finish
c# call multiple methods asynchronously
async/await c#
task.run without await
c# wait for async method to complete

I want to call sleep() function without the await keyword before it. To achieve this, I tried with another wrapper-async function sleepAsync(x, callback) with callback but it didn't work.

Here is my code -

function color() {
  let string1 = "yellow yeelow";
  console.log(string1);
  let string2 = "blu glu";
  console.log(string2);
  let string3 = "green freen";
  console.log(string3);
}

async function sleep(ms) {
  return new Promise(resolve => setTimeout(resolve, ms * 1000));
}

Now the function call where it works correctly -

async function fcall(loop = 2) {
  for (let x = loop; x > 0; x--) {
    color();
    await sleep(2);
  }
}

fcall(4);

In the above way of calling, each iteration waits for 2 sec before continuing.

Below code is where sleep() doesn't wait for 2 sec -

async function sleepAsync(x, callback) {
    callback();
    await sleep(x);
  }

  function gcall(loop = 2) {
    for (let x = loop; x > 0; x--) {
      sleepAsync(2, color);
    }
  }

  gcall(4);

What can be done here? and where am I making mistake?


An async function will not work at the exact time if the rest of the code after is not waiting for it.

When codes start running each statement will be added to a queue and the runner will start reading using that queue. An async funtion will be added to the end of the queue.

Assume this code:

let a = true;
doSomething();
doSomethingAsync();
doSomething2();

The queue to be executed can be like this:

let a
doSomething
doSomething2
doSomethingAsync

But of you use await the async code will be added to that position where you invoked it. So adding await doSomethingAsync(); will make the queue like this:

let a
doSomthing
doSomethingAsync
doSomething2

TL; DR

You use await when you want the code to be executed now, not put it at the queue.

How to Make JavaScript Sleep or Wait | by Dr. Derek Austin 🥳, JavaScript does not have a sleep() function that causes the code to wait for a to wait for 1 second between your calls to the console.log() function. from within async functions, and you need to use the await keyword with it. If the type of t is Task or ValueTask, the type of await t is void. In both cases, if t throws an exception, await t rethrows the exception. For more information about exception handling, see the Exceptions in async methods section of the try-catch statement article. The async and await keywords are available in C# 5 and later.


You can try sleep package from npm, see it at https://www.npmjs.com/package/sleep.

Understanding Control Flow with Async and Await in C#, The await keyword does not block the current thread. What do we Sleep(1000); . csharp Another way to describe it is that the thread waits synchronously. Now, for GetResult() on the task, which is a blocking call. Again� In the previous article, I discussed the best practices for ES6 promises. Picking up from where we left off, I extend the discussion to ES2017 asynchronous functions, which happen to just be "syntactic sugar" over promises.


JS has a run to completion guarantee, so if you call color() it is guaranteed to run completely There is no way to halt the execution of it in any way. There are however async functions and generator functions that can be run piecewise (meaning: till an await or yield is reached), so if you don't want to use await, you have to yield:

 function* color() {
   let string1 = "yellow yeelow";
   console.log(string1);
   yield;

   let string2 = "blu glu";
   console.log(string2);
    yield;

    let string3 = "green freen";
    console.log(string3);
  }

  async function sleep(ms) {
     return new Promise(resolve => setTimeout(resolve,  ms * 1000));
  }


  async function fcall() {
     const it = color();
     let done = false;
     while(!done) {
       ({ done } = it.next());
       await sleep(2);
     }
 }

Visual C#: Thread.Sleep vs. Task.Delay - TechNet Articles, (I am not going to explain the async and await keywords here; you can find many Delay(). Task.Delay acts in a very different way than Thread.Sleep. Delay is not blocking the calling thread so the UI will remain responsive. That way, none of your other code will have its execution postponed while still taking advantage of the exception treatment that comes with using await. Conclusion and Next Steps Although the control flow of an application is always a bit tricky when it has asynchronous aspects, knowing a bit about how await works under the hood helps a great deal.


Async/Await, Sleep(1000); } async Task MyMethodAsync() { // Do asynchronous work. await Task. Async methods returning void don't provide an easy way to notify the calling types of asynchronous programming, not just the new async/await keywords. But if we declare getPromise without the async keyword (snippet 3), we can still call it with the await keyword. The reason being is getpromise () returns a Promise object. If a function returns a Promise, we can call it with await. So snippet 3 and snippet 1 still work together.


C# Async Antipatterns, The async and await keywords have done a great job of simplifying writing Because this call is not awaited, execution of the current method The trouble is, that the caller of MyMethod has no way to await the Wait() ) opens the door to some really nasty deadlock problems. Sleep , prefer dbContext. If you start working with the async and await keywords, you should note that you don’t preclude the use of raw Promises within your TypeScript applications. The async keyword will still return a promise which can be used in conjunction with something like Promise.all(). This gives us the flexibility to run our asynchronous functions in an


Coroutines — Tornado 6.0.4 documentation, Coroutines are the recommended way to write asynchronous code in Tornado. Coroutines use the Python await or yield keyword to suspend and resume Coroutines are almost as simple as synchronous code, but without the and await keywords (functions using these keywords are also called “native coroutines”). The await expression causes async function execution to pause until a Promise is settled (that is, fulfilled or rejected), and to resume execution of the async function after fulfillment. When resumed, the value of the await expression is that of the fulfilled Promise. If the Promise is rejected, the await expression throws the rejected value.