To Async Or Not To Async

Maxwell You
maxyou
Published in
5 min readOct 20, 2018

This week’s blog post brings with it an interesting dive into the world of asynchronous code and the exciting new feature of not being required to have Spotify open when using Spotify Voice!

Asynchronous Execution and Multitasking

Source

Imagine you have a to-do list with tasks that you need to complete:

  • Buy groceries
  • Do laundry
  • Make soup for dinner

What might be an efficient way to complete all these tasks? Multitasking is a good approach!

You might start out by putting the laundry in the washer since it takes a while to complete. In the meantime, you could run to the grocery store and pick up the ingredients for your dinner. By the time you get back, the wash is complete and you can start the drying process. While that runs, you start preparing your soup by putting a pot of water to boil. As your water heats up, you prepare your soup ingredients and add them in when the water is boiling. At this point, you’ve made a mess in the kitchen, so you decide to wipe the counter and wash some dishes while the soup cooks. When the soup is ready, you pour a bowl to eat, but remember that your laundry also finished a few minutes ago! Well, it’s one of those days where you don’t have time to sit and enjoy a nice, relaxing dinner, so you decide to get the laundry and eat your soup in between folding your clothes. You throw your bowl in the sink, as it’s been a long day, and put away your laundry before hitting the hay.

You might be thinking: “Awesome Max, I still don’t know what “asynchronous” means and you’ve just made me hungry for soup!”

Source

Sorry, that was a bad joke, but allow me to explain. You might be familiar with the term synchronous which basically means sequential: doing things in order and finishing one task before moving onto the next. If you had approached the to-do list with a synchronous mindset, you probably would have focused on one task at a time: first, do the laundry, then go to the grocery store, then cook dinner. Now wouldn’t that be inefficient?

Instead, we approached the to-do list asynchronously or out-of-order: doing other things while waiting for other things to finish. Much more efficient!

Synchronous and Asynchronous Programs

Computer applications can also process things synchronously or asynchronously. Depending on the application, one might be better than the other, or maybe a mix of both is the best. In the case of Spotify Voice, the application is mostly asynchronous. Let’s take playing the spotify_voice playlist action as an example. When you click “Play New Music Friday”, Spotify Voice sends a request to Spotify for the playlists on your account. While waiting for the response, Spotify Voice could call other functions, but in this case, it is necessary that we wait for the response before continuing execution. This is because we need to look through the list of playlists returned in the response so the spotify_voice playlist can be found, and this would not be possible if we went and did something else in the interim. Once the playlist has been found, a request to play the playlist is sent to Spotify and playback starts shortly afterwards.

Okay… so I just said that Spotify is mostly asynchronous, but then explained the execution of playing the spotify_voice playlist is basically synchronous. What’s the deal? It is true that the Spotify API calls are all asynchronous in nature because other code can be executed while a request is being processed on Spotify’s end. For example, I could have executed code for displaying a loading symbol while the spotify_voice playlist was created: when Spotify sends a response on the status of creating the playlist, I could remove the loading symbol and display the status of playlist creation instead. For something like playing the spotify_voice playlist, however, I needed the intermediate actions to happen synchronously since requesting for a playlist to be played requires the name of the playlist which, in turn, requires us to get the playlists first.

(A)synchronous Shuffling

You may be wondering what an example of asynchronous behavior is that is actually in Spotify Voice. When the spotify_voice playlist is played, I wanted the playback to be shuffled. In order to shuffle your playback, however, requires that there be an active playback session (i.e. you are listening to music). So, this has to be done after a play request is issued to Spotify. However, when a play request is issued, Spotify issues a response immediately if it received the request successfully. This response does not indicate that playback has started though, just that Spotify received the request and is working on starting playback. The play request has this behavior because Spotify doesn’t want your app to wait while it turns on playback on your device. Spotify Voice needs to wait until playback has started in order to shuffle the tracks, so I set the app to wait for 1 second before issuing the shuffle API call to Spotify. Okay, so I basically just turned the asynchronous behavior into a synchronous action again… The moral of the story is, for Spotify Voice, I needed many of the actions to act synchronously, but their default behavior was asynchronous, so the code ended up being more synchronous in structure.

Improvement to the User Experience

Source

On a less confusing note, let’s talk about the awesome new feature that got implemented this week: headless Spotify music playback! Oh no, I just said less confusing, then threw out some weird phrase… Don’t worry, this basically means that you no longer need to have Spotify open and play/pause a song before being able to use Spotify Voice! Now, when you click “Play New Music Friday”, the tracks will start playing immediately! Of course, clicking the media control buttons first will still not start playback since there is no active device yet, so always click “Play New Music Friday” first when using Spotify Voice.

This feature was possible through the relatively new Web Playback SDK from Spotify. Basically, a “virtual” device is created when Spotify Voice is opened and music playback is done through this virtual device (removing the need to have the Spotify application open). This is probably one of the improvements I’m most excited about since the previous way of setting up Spotify Voice was very unintuitive and now it just works as you would expect.

Song of the Week

Happier by Marshmello ft. Bastille [Spotify][YouTube]

--

--