Understand Postman Flows with Advanced Examples

Understand Postman Flows with Advanced Examples

Postman Flows is an API workflow builder for logically connecting APIs. Use Flows in your Postman workspace to chain requests, handle data, and construct real-world workflows.

Postman Flow is a free beta feature available to all Postman members. To get started, go to the Postman web app or download the newest version of the Postman desktop client.

Before I proceed further in this post covering some of the advanced use cases implemented through Postman Flow, it is highly recommended to go through my previous video.

In my previous video, I have covered the basics of Postman flow as well as implemented a basic use case. You can watch the video below.

Down Arrow

Today I will be covering two advanced use cases implemented through Postman Flow. I will be using some free APIs and Postman blocks to implement the use cases. So without any further delay let's start -

Use Case 1: Consolidating responses from multiple web services

I will be using the free Bored API which recommends activities based on categories.

Here are the steps -

    • Step 1 - Call Bored API to get activity recommendation of type recreational.
    • Step 2 - Call Bored API again to get another activity recommendation of type recreational.
    • Step 3 - Create a list containing recommendations from both call 1 and 2.
    • Step 4 - Finally print the list in the Terminal.

Watch the video below to understand how I have implemented this use case in Postman Flow. The blocks I have used from Postman are - Send Request, Create Data and Terminal

Use Case 2: Introducing the delay

I will be using the free Bored API which recommends activities based on categories and modify the previous implementation a little bit to introduce the delay

Here are the steps -

    • Step 1 - Call Bored API to get activity recommendation of type recreational.
    • Step 2 - Introduce the delay of 10 seconds
    • Step 3 - Call Bored API again to get another activity recommendation of type recreational after 10 seconds.
    • Step 4 - Create a list containing recommendations from both call 1 and 2.
    • Step 5 - Finally print the list in the Terminal.

Watch the video below to understand how I have implemented this use case in Postman Flow. The blocks I have used from Postman are - Send Request, Delay, Create Data and Terminal.

Use Case 3: Loop through the result

I will be using the free Cocktail Database API which recommends cocktail drinks based on ingredients.

Here are the steps -

    • Step 1 - Call Cocktail API to get cocktail drinks based on the ingredient Vodka.
    • Step 2 - Loop through the result
    • Step 3 - Create variable to store the cocktail drink name
    • Step 4 - Finally print the name in the Terminal.

Watch the video below to understand how I have implemented this use case in Postman Flow. The blocks I have used from Postman are - Send Request, For Each, Create Data and Terminal.

Use Case 4: Validate the result

I will be using the free Open-Meteo API which returns the weather information based on the location.

Here are the steps -

    • Step 1 - Call Open-Meteo API to get Vancouver weather information.
    • Step 2 - Call Open-Meteo API to get New York weather information.
    • Step 3 - Validate whether Vancouver's Windspeed is less than equal to New York's Windspeed
    • Step 4 - If yes, print Vancouver's weather information.

Watch the video below to understand how I have implemented this use case in Postman Flow. The blocks I have used from Postman are - Send Request, Check and Terminal.

Conclusion

I think this new feature is beneficial for implementing multiple use cases in more drag and drop approach. Flows is in beta, and the Postman team is continuously working on it. I would love to hear about your flow creations, and any bugs you've run into while working with Postman flow.

Comments

Fill an Array with Initial Values in JavaScript

Fill an Array with Initial Values in JavaScript

Arrays can be initialised with data in a variety of ways in JavaScript. Let's examine which methods are the easiest and most popular in this post.

 

Read this blog post or watch the video below to learn more about different ways to fill an array with initial values in JavaScript.

#1 Fill an array with primitives

Let's say, you want to initialize an array of elements 5 with the number 1. You can use the array.fill(initialValue) method to initialize an array. This method will return the modified array.

The thing that you need to keep in mind is the usage of array.fill(initialValue) in combination with Array(n) constructor.

const length = 5;
const newArray = Array(length).fill(1);
console.log(newArray); // Outputs [1,1,1,1,1]

Array(length) creates the sparse array. If you are not aware of sparse array, please read my previous blog: Sparse vs Dense Arrays in JavaScript. So Array(length).fill(1) basically modifies the array by putting 1 in every index of the array and finally returning the array like [1, 1, 1, 1, 1].

Array(length).fill(initialValue) is a convenient way to create arrays with the desired length and initialized them with a primitive value (number, string, boolean).

#2 Fill an array with objects

What if you need to add objects to an array? Whether you want the array filled with the initial object instances or with other instances, this need is significantly complicated.

#2.1 array.fill(initialValue)

If you want to initialize the array with the same object instance, then you can use array.fill() method.

const length = 4;
const myArray = Array(length).fill({ stage: 1 });
console.log(myArray); //[{stage : 1},{stage : 1},{stage : 1},{stage : 1}]

Array(length).fill({stage:1}) creates an array with length 4 and at the same time put the same object i.e. {stage : 1} on each index of the array.

The important thing that you need to remember is that each of the index is filled with same object instance. So if you want to change one index of the array, it will not only change that index, rather it will change every index of the array. Refer to the program below 

const length = 4;
const myArray = Array(length).fill({ stage: 1 });
console.log(myArray); //[{stage : 1},{stage : 1},{stage : 1},{stage : 1}]
myArray[2].stage = 3;
console.log(myArray); // [{stage : 3},{stage : 3},{stage : 3},{stage : 3}]

#2.2 array.from()

If you want to fill the array by creating copies from another array, then you can use array.from() method.

Array.from(array, mapperFunction) accepts two arguments: an array (from which elements will be copied) and a mapperFunction (a function which will be called for each element of the original array).

Array.from() will invoke the mapperFunction on each item of the array and then pushes the result to a new array, and finally the newly mapped array.

const length = 4;
const oldArray = Array(length).fill({ stage: 1 });
console.log(oldArray); //[{stage : 1},{stage : 1},{stage : 1},{stage : 1}]
const newArray = Array.from(oldArray,()=>{return {stage : 4}});
console.log(newArray); //[{stage : 4},{stage : 4},{stage : 4},{stage : 4}] 

With this approach, if you change some element of the array, only that element is changed, not the entire array. 

const length = 4;
const oldArray = Array(length).fill({ stage: 1 });
console.log(oldArray); //[{stage : 1},{stage : 1},{stage : 1},{stage : 1}]
const newArray = Array.from(oldArray,()=>{return {stage : 4}});
console.log(newArray); //[{stage : 4},{stage : 4},{stage : 4},{stage : 4}]
newArray[2].stage = 5;
console.log(newArray); //[{stage : 4},{stage : 4},{stage : 5},{stage : 4}]

#2.3 array.map() with spread operator

When you use Array.length() to create an array, it creates a sparse array and using map() function on the sparse array skips the empty indexes from the array.

But using the spread operator you can transform a sparse array into an array with items initialized with undefined. After that, you can apply array.map() method on that array.

const length = 4;
const myArray = [...Array(length)].map(() => {
 return { value: 10 };
});
console.log(myArray); // [{ value: 10 }, { value: 10 }, { value: 10 }, { value: 10 }]

The expression [...Array(length)] creates the array where items are initialized with undefined. And then applying map() to create new instances.

Conclusion

There are several good approaches to fill an array with initial values in JavaScript.

If you want to create an array using primitive values, the best method is to use Array (length). fill(initialValue).

If you want to create an array with object instances but don't care if each item has the same object instance, use Array (length). The method to use is fill(initialObject).

Otherwise, you might use Array.from(Array.length, mapper) or [...Array(length)] to populate an array with distinct object instances. map(mapper), where mapper is a function that, on each invocation, returns a new object instance.

Comments

Sparse vs Dense Arrays in JavaScript

Sparse vs Dense Arrays in JavaScript

Arrays in JavaScript is a very important concept. Developers are dealing with Arrays like creating, manipulating etc almost every day while implementing logics. Arrays are very easy to understand in JavaScript.

An array is a special variable, which can hold more than one value

But do you know that Arrays can hold nothing i.e. it is possible to create hole in an Array?

In this post, I will be discussing about Sparse and Dense Arrays. Also I will be talking about how to create them and what you can do with this type of arrays.

I have written a blog post and published Youtube playlist, where I have discussed different Array methods in JavaScript.

Read this blog post or watch the video below to learn more about the Dense vs Sparse Array.

Dense Array

An array in JavaScript is an object representing an ordered collection of items.

Let's consider the below array of countries.

const countries = ['Canada', 'Switzerland', 'India'];
console.log(countries[0]); // output 'Canada'
console.log(countries[1]); // output 'Switzerland'
console.log(countries[2]); // output 'India'
console.log(countries.length); // logs 3

Items in an array can be accessed by the property "Index". Index starts with the number 0 i.e. the first element of the array can be accessed by index value as 0. From the above example, countries[0] will return the first element of the countries array which is 'Canada'.

There is another property named 'length' which indicates the number of elements present in an array. For the example above, length will return 3 as there are three elements.

Now an array will be considered a Dense Array when it will have elements in each index of the array starting from 0 till length-1.

Below is the function which determines whether an array is Dense or not.

function isDense(array) {
 for (let index = 0; index < array.length; index++) {
 if (!array.hasOwnProperty(index)) {
 return false;
 }
 }
 return true;
}
const countries = ['Canada', 'Switzerland', 'India'];
console.log(isDense(countries));

With this above function, what do you think? isDense() method will always return true i.e. all arrays are dense or there might be some situations where isDense will return false. Let's find out

Sparse Array

When you have holes in the array, that array will become a Sparse Array. Bu hole means there are some indexes in the array which hold nothing.

With this definition, let's check out the below code snippet which is now dealing with a sparse array.

const countries = ['Canada', , 'India'];
console.log(isDense(countries));

This time, isDense() method will return false because the second element of the array in black. Trying to access index 1 from the array will return undefined.

To check explicitly whether there's a hole at a specific index you need to use index in countries expression.

const countries = ['Canada', , 'India'];
console.log(0 in countries); // output true
console.log(1 in countries); // output false

Ways to create sparse array

There are many ways to create a sparse array. Let's explore them below

# 1

When employing the array literal without a value, a sparse array is created.

const countries = ['Canada', , 'India'];
console.log(countries); //Outputs "Canada", undefined, "India"

# 2

A sparse array is created by calling Array(length) or new Array(length) (with a number argument).

const countries = Array(3);
console.log(isDense(countries)); // outputs false
console.log(countries);          // outputs [undefined, undefined, undefined]

# 3

Using delete array[index] operator on the array will create sparse array.

const countries = ['Canada', 'Switzerland', 'India'];
delete countries[1];
console.log(isDense(countries)); // outputs false
console.log(countries);          // outputs ['Canada', undefined, 'India']

In the above example, countries was initially a dense array. But the moment we deleted the content of the array from index 1, it became a sparse array.

# 4

By increasing the length property of the array, we can create holes in the array and thus a sparse array.

const countries = ['Canada', 'Switzerland', 'India'];
countries.length = 4;
console.log(isDense(countries)); // outputs false
console.log(countries); // outputs  'Canada', 'Switzerland', 'India', undefined

Different array methods working with sparse array

While dealing with sparse array, you need to keep in mind one thing, that many array methods skip the holes/undefined elements from the sprase array.

const countries = ['Canada', , 'India'];
countries.forEach(country => {
 console.log(country);
});
//This will output Canada India

Conclusion

In JavaScript, an array can be dense or sparse.

If there are items at each index from 0 to array.length - 1, the array is dense. Otherwise, the array is sparse if at least one item is absent at any index.

Comments