Waiting for Asynchronous Function Call to Complete

What if we do not wait for an asynchronous javascript function?

It's run just the same. (In fact, you never await a function, you await for the the Promise it returns.)

The asynchronous function is run synchronously until the first await or return within it, at which point a Promise is returned to the caller and the rest of the function is arranged to run later.

It's up to the caller to do something (or nothing) to the Promise. After all, you might wish to store the promise in an array and await for the lot of them (Promise.all) or do something more esoteric about it, so JavaScript itself doesn't care.

Some smart enough IDEs and linters are able to raise a warning about unhandled promises, though, especially if you have enough type information to do so (e.g. by using TypeScript).

Javascript - wait for async call to finish before returning from function, without the use of callbacks

Sadly, it is pretty much impossible to wait for async code in a synchronous way. This is because there is no threading in JS (most JS runtimes, but some are). So code is either synchronous or asynchronous.

Asynchronous code is possible because of the event loop. The event loop is part of the javascript runtime. It works by keeping a stack of callback functions that run when events trigger them - usually either timeout events (which you can set with setTimeout()) or IO events (which happen when you make disk or HTTP requests, or on user interaction). However, these callbacks only run when no other code is running, so only when the program is idle and all functions have returned.

This means that techniques like "spin loops" (where you just run a loop until a condition is changed by another thread) that work in threaded environments don't work because the async code won't run until the spin loop finishes.

More Info: https://medium.com/front-end-weekly/javascript-event-loop-explained-4cd26af121d4

How to wait for async method to complete?

Avoid async void. Have your methods return Task instead of void. Then you can await them.

Like this:

private async Task RequestToSendOutputReport(List<byte[]> byteArrays)
{
foreach (byte[] b in byteArrays)
{
while (condition)
{
// we'll typically execute this code many times until the condition is no longer met
Task t = SendOutputReportViaInterruptTransfer();
await t;
}

// read some data from device; we need to wait for this to return
await RequestToGetInputReport();
}
}

private async Task RequestToGetInputReport()
{
// lots of code prior to this
int bytesRead = await GetInputReportViaInterruptTransfer();
}

I need not wait until executing async method call()]

The main thread is blocked on the call to future.get() until the task completes. Remove this call and your second log statement will print immediately.

That addresses what you asked exactly. But I doubt it is what you want.

The purpose of submitting an asynchronous task is to permit the main thread to carry on immediately with other work. If the main thread requires the result of the task to proceed, the task is not a candidate for performing asynchronously.

Instead, add the processing of the result to the asynchronous task itself. Then the main thread does not have to wait for the result.



Related Topics



Leave a reply



Submit