Asynchronous Method Calls – An Introductory Guide

0
1516

Whether you are a mobile or web developer, the topic of asynchronous method calls has probably come up at some point on your coding journey.

So what exactly are asynchronous method calls and why should you care about them? Since technically that was two questions, let’s take them one at a time.

What are Asynchronous Method Calls?

In order to understand what asynchronous method calls are, it helps to go through an example. Imagine that you are posting a picture to Instagram.

asynchronous method calls

If you use Instagram you know that after you create a post:

  1. It shows you a loading icon. While your image is being uploaded to the Instagram servers you can view other posts.
  2. If your post is created successfully it gives you a success notification.
  3. Whereas if your post has an error it says that the photo couldn’t be uploaded and asks if you want to try again.

Believe it or not, this workflow will teach you everything (at a high level) that you need to know about asynchronous method calls as a developer. Let’s take a look at the process step by step.

Step 1

During step 1, when Instagram shows the loading icon and allows you to view other posts, the app is making what’s called an asynchronous request to the Instagram server. It’s considered an asynchronous request because it’s performing an action. In this case it’s uploading a file, while not blocking other method calls, such as using other parts of the app.

Step 2

In step 2 Instagram is sending a response back to the application. When the app sent out the request in step 1 it created what’s known as a promise. The promise expects some type of response from the server. Typically either a success message or an error. In the case of step 2, the promise is ready and waiting to catch the request as it comes back from the server.

And since the response contains a success message Instagram proudly lets the user know that their post is now live.

Step 3

In step 3 the application needs to know what to do when a failure occurs. Imagine how bad of an experience it would be if you uploaded an image to Instagram but it didn’t tell you if there was a failure.

In order to ensure that the user and the application know when issues arise, the server response in step 3 would contain an error message. From that point it would be the app’s job to parse the response and give the user feedback.

Recap

As a recap, asynchronous method calls are tools you can use to communicate with outside services while not stopping the flow of the application. Typically asynchronous method calls work with promises. Which mean that they wait for server responses to confirm that the process completely properly or an error message if there was an issue.

Why are Asynchronous Method Calls Important?

So now that you what asynchronous method calls are, why are they important? Well, let’s imagine a world without asynchronous behavior.

A World Without Asynchronous Method Calls

Unlike our streamlined example of Instagram, let’s imagine that YouTube didn’t allow for asynchronous method calls. And let’s follow that up by imagining that you had thousands of videos to upload to the site.

In the dark ages of the web before asynchronous method calls became popular, this scenario would have been very ugly. Because the system wouldn’t allow you to perform two actions at once, you’d have to open a new page for each video you want to upload. From there you’d need to start an upload from scratch.

Instead of being able to upload multiple files at a time, like YouTube lets you do now, you’d be forced to take days to upload all of your videos. And then you’d have to monitor each of the open pages in case an error occurred. Needless to say this would not make for a good user experience.

Mobile Apps

When I was teaching myself how to build iOS applications my knowledge of asynchronous method calls really came into play. If you are building mobile applications that communicate with outside servers a thorough understanding of asynchronous methods is required.

Summary

I kept this guide very high level. The main reason is because each language and framework has a different configuration for how asynchronous method calls work. In the show notes I’ve included links for some popular frameworks and how to implement asynchronous behavior.

Show Notes

LEAVE A REPLY

Please enter your comment!
Please enter your name here