-
JavaScript Export and Import
Having a complex application makes developers scroll through hundreds and thousands of lines of code, which makes it harder for them to debug or even understand the app. Luckily, JavaScript allows overcoming such difficulties. Having import and export helps one out of the problems above. There are several syntax options for export and import directives. In the…
-
JavaScript Modules
With growing the application more extensive, there is a need to split it into multiple files: modules. As a rule, a module includes a class or a library of functions. For some time, JavaScript existed without a language-level module syntax. That wasn’t a difficulty as the scripts were small and straightforward. But, over time, the scripts became more…
-
JavaScript Async Iterators and Generators
Asynchronous iterators allow iterating over data, which comes asynchronously. For example, downloading something chunk-by-chunk over the network. With asynchronous generators, it becomes more convenient. To be more precise, let’s start at grasping the syntax. Async Iterators Async iterators are, generally, like regular iterators. But, there are several syntactical differences. A regular iterable object is the…
-
JavaScript Generators
In this chapter, we are going to observe the JavaScript generators. Generators are functions that you can use for controlling the iterator. In comparison with regular functions that return a single value or nothing, generators return multiple values in a sequence, one after another. They operate great with iterables and allow creating data streams with ease. Generator Functions For…
-
JavaScript Async/await
There exists a unique syntax that can enhance your performance with promises. It’s async/await: a surprisingly easy and comfortable means to deal with promises. Async functions The async function declaration specifies an asynchronous function, which can return an AsyncFunction object. Async functions perform in a separate order than the rest of the code through the event loop and return a Promise as…
-
JavaScript Microtasks
JavaScript promises use the microtask queue for running callbacks. This chapter is dedicated to the exploration of microtasks in JavaScript. Before getting to microtasks it is useful to define JavaScript tasks. A task is considered a JavaScript code that is scheduled to be executed by standard mechanisms. For instance, beginning to run a program, an event callback that…
-
JavaScript Promisification
Promisification is a long term for a straightforward transformation. It represents a conversion of the function, which accepts a callback into a function returning a promise. Often, transformations like that are needed in real-life because multiple libraries and functions are based on callbacks. But, promises are much more convenient. Let’s consider a load_Script(src, callback): Now, we are going to promisify…
-
JavaScript Promise API
As we have already stated, promises in JavaScript represent the eventual completion or failure of an asynchronous operation, as well as its resulting value. The Promise class comprises five static methods. In this chapter, we cover all of them in detail. Promise.all The Promise.all method is used when it is necessary to execute many promises at the same time and wait…
-
JavaScript Error Handling with Promises
JavaScript promises allow asynchronous code to use structured error handling. The promise chains are served as a great way of error handling. Whenever a promise rejects, the control jumps to the nearest rejection handler. One of the most useful methods of error-handling is .catch. catch As it was already mentioned, .catch is one of the most useful methods for error handling in…
-
JavaScript Promise Chaining
Let’s suppose that you have a sequence of asynchronous tasks to carry out one after another. For example, loading scripts. The JavaScript promises to introduce a couple of receipts to perform that. This chapter is dedicated to promise chaining, which is demonstrated in the example below: The notion is that the result passes through the chain of .then handlers.…
