Promises & Why Your Code Won’t Keep Them

What on earth is async/await syntax and how do I use it?

Async

async functions return a promise, where this promise allows for other code in your program to run while waiting for your asynchronous function to complete. Otherwise, you need to specify to your program that you’d like to wait for an async function to finish execution before moving on.

Await

If you don’t await an asynchronous function’s completion, it’ll simply say “well, I promised I’d return something to you eventually, so here is an [object Promise].” However, when attempting to immediately manipulate these functions’ return values, it’s best you wait for promises to be resolved before continuing. And that’s when we use async/await.

Inside the Function

Below, I’ve initialized an asynchronous function entitled createSheet(), inside which I use the Google Sheets API to create a spreadsheet, and return it’s URL. Here’s what I found to be successful:

async function createSheet() {
let response = await sheet.spreadsheets.create(request);
return response.data.spreadsheetUrl;
}

Calling the Function

Since createSheet() is defined as async, calling using the await keyword signifies that we want our code to wait for createSheet() to fully finish executing before continuing on to other code.

return await createSheet(/**parameters**/);

Breaking the Flow

Here’s an explicit example of how, without using await, your code will not know when to wait for what:

var url = ‘’;let response = createSheet(/**parameters**/).then(res => {
url = res.data.spreadsheetUrl;
console.log('one');
});
console.log('two');
return url;

Big nerd writing about the intersection between technology & fashion. Spanish/Cuban turned New Yorker. Founder & Editor at Dashion: medium.com/dashion.