Posts

Showing posts with the label javascript

Calling APIs in sequence using async-await

Image
 There are certain cases when we want to call some APIs in a sequence (order) to get some data. For example, let's say we want to fetch data of 1000 students of an university for some analysis. The API given to us fetches only 100 records at a time but supports pagination. Therefore, we need to call the API 10 times, one after the other to get these records. This can be achieved by Using plain old callbacks. Using a combination of promises and callbacks. Using modern JavaScript feature - async-await (both async and await are keywords). In this article we will discuss method 3 to solve this problem. We will see the implementation in action using an open API. We will be creating a console app using NodeJS (version 14.7 as of writing) and will be using axios  for making http calls. Setting up the project Create a directory where you want to keep your JavaScript code files. I have named my directory "api-sequence-async-await". Open your terminal or command prompt and run co

What is object destructuring and spread operator in JavaScript?

Object destructuring Object destructuring is a feature in JavaScript which allows us to assign values from arrays and properties from objects to distinct variables. This is also referred to as destructuring assignment . Examples Swapping two numbers let   x  =  5 ,  y  =  7 ; [ x ,  y ] = [ y ,  x ]; console . log ( x ,  y );  // 7 5 Taking out values from an array and assigning them to variables let   array  = [ 1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ]; const  [,  a , , ,  b ,  c ] =  array ; console . log ( a ,  b ,  c );  // 2 5 6 Assigning remaining values to a variable let   array  = [ 1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 ]; const  [ x , ... y ] =  array ; console . log ( x ); console . log ( y ); // 1 // [ //   2, 3, 4, 5, //   6, 7, 8, 9 // ] Taking out properties from an object and assigning them to variables const   student  = {      name:   'Student 1' ,      rollNo:   45 ,      subject:   'Economics' ,   

What are filter, map and reduce functions in JavaScript?

These are in built functions which are available on arrays in JavaScript. In this article, we will talk about what these functions do and how they are used. filter – Array.prototype.filter This function returns a subset of an array containing those elements which satisfy a given condition in the form of a callback . Syntax newArray = someArray.filter(callback(element[, index, array])[, thisArg]) newArray is the filtered array. someArray is the original array. callback is the callback function. element is the current element of the array which is being processed by the filter function. index is the index of the current element. array is the original array. thisArg is the value to be used as this while executing the callback. If the callback function returns a truthy value then the element will be included in the new array otherwise it will be ignored. Examples Getting even numbers from a list of numbers let   numbers  = [ 1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 ,