Async & Await Keywords
By: Charles Tillmon

Async & Await Keywords

When ES2017 was released along with it came the async and await keywords. These keywords were developed to make working with promises easier or what is referred to as syntactic sugar. When used theses help your asynchronous code look like traditional synchronous code structure and will help minimize the chaining needed to complete task. In theory this should make your code easier to digest. To be clear these keywords do not replace promises but are supplemental to make your code more efficient.

The async keyword can be pre-fixed to a function declaration to define it contains asynchronous code. These functions will always return a promise as a result. Inside of these async defined functions you will see the use of the await keyword. The await keyword will pause the execution of an async function until a resolved value is returned. This pausing of execution will not cause blocking behavior because the event loop is free to perform other tasks. This means that you will still be able to interact with other parts of the application.

Another noticeable difference will be experienced with error handling. These keywords are typically associated with try catch blocks to catch errors over various lines of code. The alternative would be chaining a catch method to each individual line of code, which as you can expect would get messy very quickly. However, like we always mention this should not be treated as a one solution solves all approach. Understanding these concepts and the underlying promises they represent will help in knowing how to properly implement these tools. 

要查看或添加评论,请登录

Charles Tillmon的更多文章

  • CSS: Normal Document Flow

    CSS: Normal Document Flow

    As we focus more on understanding the styling layer of web pages it is only natural that we discuss the normal document…

  • JavaScript Promises

    JavaScript Promises

    Typically, with asynchronous code you will see the use of callback functions, which are functions that are passed as…

  • TCP/IP Overview

    TCP/IP Overview

    Have you ever wondered how data is transmitted between computers? Well just like us they must establish a common…

  • The Internet vs. The Web

    The Internet vs. The Web

    Have you ever used the Internet and the Web interchangeably? This is a common mistake that many have made, me included.…

  • Core Front-End Components Overview

    Core Front-End Components Overview

    Have you ever wondered how web pages get on the web? When it comes to basic web pages, typically they consist of 3 core…

  • Asynchronous vs. Synchronous Code

    Asynchronous vs. Synchronous Code

    JavaScript is a single threaded, synchronous programming language. This means when code is executed by the JavaScript…

  • Is Modernizing Your Business Worth The Investment?

    Is Modernizing Your Business Worth The Investment?

    “The day that you plant the seed is not the day that you eat the fruit”. This is a mantra that can apply to life, but…

  • JavaScript Basics-Functions: Part 4

    JavaScript Basics-Functions: Part 4

    JavaScript functions are blocks of code that either perform a task or return a value. These blocks of code are executed…

  • JavaScript Basics-Loops: Part 5

    JavaScript Basics-Loops: Part 5

    Loops in JavaScript are a way to repeat the same set of actions a set number of times or until a condition is met…

  • JavaScript Basics-Conditional Statements: Part 3

    JavaScript Basics-Conditional Statements: Part 3

    Sometimes in programming languages the code needs to make a decision on what actions to take dependent upon the…

社区洞察

其他会员也浏览了