Image for post
Image for post

ES2017 introduced handy syntax for fixing nested callbacks called async-await expressions. It is an alternative for coding sequential Promise-calls, previously possible only by then-chaining pattern.

Motivation behind using Promises is to write code for asynchronously called functions in more readable manner, so it doesn’t contain nested code blocks, but instead position such function-call statements on the same level.

Practical example

Here is code for reading inputs from command-line written with nested callbacks:

Notice two level deep for rl.question calls. The first call shows ‘What is your name’ question and, when answered, displays ‘Where do you live’.

After wrapping API function in…

Image for post
Image for post

In front-end programming often happens that user-interaction with one component should update other components. Eventually this leads to twisted architecture, because links between components are difficult to maintain when introducing a new component or removing one.

The solution is to introduce a specific central component that doesn’t do any visual rendering, but instead holds data referenced during a rendering by the rest of the components. The holding data central-component is called model-component. Visual components that render data from a model are called view-component. Input components that receive input-data from a user are named controller components.

Controller components directly updates the…

JavaScript runs in event-driven manner. Meaning that it assigns specific functions to be called after a certain event has been happened. These sort of events might be a user interaction (like pressing keyboard button, moving a mouse), server-side actions (receiving data from a server request), time-related actions, etc. Like these one:

In many cases we need to assign an event-handler function after some other event-handler was called. This is done by nesting a function into an another.

Nested functions might be hard to read.

Promises are another syntax for defining callback functions chains, so they can be executed…

Sergii Riabokon

Programmer from Lviv (Ukraine)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store