This post is about using async/await in every possible scenario I encountered. The whole concept behind the use of async/await is to wait without blocking.

As a .NET developer I very soon embraced the concept of iterators, thanks to Jeffrey Richter’s AsyncEnumerator. I also remember an article about state machines by Matt Pietrek.

I first heard about Matt many, many years ago when I bought Undocumented Windows. Bit this is another story.

So, the whole idea here is to pause your program execution while waiting for the result of an input/output request. In a single threaded environment this allows for other work to be processed by the event loop. In a multi-threaded envionment, this allows to consume less threads. If yo want to deep dive, just read this awesome article by Stephen Cleary: There is no thread. In another life I wrote Terminate and stay resident programs on MS-DOS. Yet another story to tell some day.

I am so used to async/await now that using promises in JavaScript and/or dealing with callbacks even in c# is both difficult to read and follow mentally.

I like my programs to run sequentially.

The overall experience has been great so far, but in less than 2 weeks I encountered the same kind of issue twice. One in React Native and one in Angular. This has nothing to do with the concept itself, but this convinced me to publish this post that was waiting in the drafts.

Xamarin.Android/WPF/React Native (JS/TS)

It should work as expected with the same set of recommandations that the ones for .NET

For instance you could have in React Native:

  public async componentDidMount() {
    await super.componentDidMount();
    logging.logWithTimestamp("entering Capture::componentDidMount()");
    // other await calls here...

 

React Native

I’ve had a problem in React Native where an async call never made it up back to JS on  Android. I scratched my head for several hours before looking at the underlying Java method to pinpoint the issue.

Angular

Down the rabbit hole this time with an async call lost in translation.

RxJs

I had an interresting use case combining RxJs and async/await

The resulting code is the following:

          this.emitter = fromEventPattern(
            this.addBleHandler,
            this.removeBleHandler,
            (err, char) => [err, char]
          );
          // tslint:disable-next-line:max-line-length
          // https://stackoverflow.com/questions/55835700/how-to-make-an-rxjs-subscription-with-an-async-function-serialize-the-onnext-cal
          this.rxSubscription = this.emitter
            .pipe(
              concatMap(value =>
                this.handleUpdatedValuesComingFromSensorThroughBle(value)
              )
            )
            .subscribe();

Final words

If you’re wondering who did it first, take a look at this answer.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.