Asynchronous Data Pulling From Web APIs in JavaScript
Last Updated on January 5, 2024 by Editorial Team
Author(s): LucianoSphere (Luciano Abriata, PhD)
Originally published on Towards AI.
Learning fetch, async, await, and promises with code that pulls cryptocurrency historical values for multiple coins in a single shot
Image composed by the author from free Dall-E3 generations
In JavaScript programming, asynchronous operations play a crucial role in running pieces of code that would otherwise block execution if run in the βstandardβ way through the main thread. One particular application of asynchronous operations is pulling data from external sources by calling APIs, as these calls will of course take some time to respond and the JavaScript main thread cannot just halt execution while waiting for the response. Even a very fast API that takes just a couple of milliseconds to reply will screw up the program flow if not handled properly.
Being the language of the web, JavaScript could not skip providing tools to handle API calls properly. In this new article I will delve into the key concepts of fetching data asynchronously using the fetch() function, async and await keywords, and the mechanism of βpromisesβ.
To illustrate these concepts, I will show you how they work together at the core of a web app that pulls cryptocurrency data and then performs some basic calculation, here Fibonacci retracements levels, on the fetched data. Of note, asynchronous operations in JavaScript allow this example web app to pull data for multiple coins in parallel,… Read the full blog for free on Medium.
Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming aΒ sponsor.
Published via Towards AI