The Wayback Machine - https://web.archive.org/web/20240915011059/https://www.geeksforgeeks.org/how-control-flow-controls-the-functions-calls/
Open In App

How “Control Flow” Controls the Functions Calls ?

Last Updated : 18 Jun, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Control flow is a fundamental concept in programming that determines the order in which code statements and function calls are executed. By controlling the flow of a program, you can create complex logic, handle different execution paths, and manage the sequence of function calls. Understanding control flow is crucial for writing efficient and maintainable code.

This article explores how control flow controls function calls in programming, covering key concepts, common patterns, and practical examples.

What is Control Flow?

Control flow refers to the order in which individual statements, instructions, or function calls are executed or evaluated in a programming language. Control flow mechanisms allow a program to make decisions, repeat operations, and jump to different sections of code based on certain conditions.

Key Control Flow Mechanisms

  • Sequential Execution: Code is executed line by line, from top to bottom.
  • Conditional Statements: if, else if, and else statements allow code to be executed based on specific conditions.
  • Loops: for, while, and do...while loops enable repetitive execution of code blocks.
  • Function Calls: Functions are invoked based on the program’s flow and the logic defined.
  • Exception Handling: try, catch, and finally blocks manage errors and exceptions in the program.

Controlling Function Calls with Control Flow

Function calls are integral to the control flow in programming. They enable modular and reusable code. By controlling when and how functions are called, you can manage the complexity of your program.

Sequential Function Calls

In sequential execution, functions are called one after another in the order they are written.

JavaScript
function greet() {
    console.log('Hello');
}

function farewell() {
    console.log('Goodbye');
}

greet();
farewell();

Output
Hello
Goodbye

Note: In this example, greet() is called first, followed by farewell(), resulting in the output

Conditional Function Calls

Functions can be called conditionally based on the outcome of conditional statements.

JavaScript
function showSuccessMessage() {
    console.log('Operation successful');
}

function showErrorMessage() {
    console.log('Operation failed');
}

const operationSuccess = true;

if (operationSuccess) {
    showSuccessMessage();
} else {
    showErrorMessage();
}

Output
Operation successful

Note: If operationSuccess is true, showSuccessMessage() is called. Otherwise, showErrorMessage() is called.

Control flow in Node.js is typically managed using one of three methods: callbacks, promises, and async/await.

Callbacks

Callbacks are functions that are passed as arguments to other functions and are executed when that function completes its task. In Node.js, many functions are asynchronous, and they often take a callback as an argument. When the asynchronous operation is completed, the callback is executed, allowing the program to continue its execution.

Steps to use a callback

  • Define the function that accepts a callback parameter. This function should perform some asynchronous operation and then call the callback with the result.
  • When calling the function, pass a callback function as an argument. This callback will be executed once the asynchronous operation is complete.
  • Within the callback function, you can handle the result of the asynchronous operation.

Syntax:

function functionName(param1, param2, callback){
// Asynchronous operation
callback(result);
}

Example 1: Implementation to show the use of callback with an example.

JavaScript
const multiply = (a, b, callback) => {

    // Multiply the values of a and b
    const result = a * b;

    // Pass the result to the callback function
    callback(result); 
}

multiply(2, 3, (result) => {

    // Log "Callback" to the console
    console.log("Callback"); 

    // Log the result with a prefix "Ans: "
    console.log("Ans: " + result); 
});

Output
Callback
Ans: 6

In the above example, the multiply function takes two numbers as arguments and a callback function that is executed when the multiplication is complete. The callback function simply logs the result to the console. The arrow function syntax is used to define the multiply function and the callback function.

Example 2: Implementation to show the use of callback with an example.

JavaScript
// Function that performs an asynchronous operation
// and invokes a callback when done
const fetchData = (url, callback) => {

    // Simulating an asynchronous operation 
    // (e.g., making an API request)
    setTimeout(() => {
        const data = { id: 74, name: "Geeks for Geeks" };
        callback(data);
    }, 2000); // Simulating a 2-second delay
}

// Callback function that handles the fetched data
const handleData = (data) => {
    console.log("Fetched data:", data);
}

// Call the fetchData function with the
// URL and the callback function
fetchData("https://example.com/api/data", handleData);

Output
Fetched data: { id: 74, name: 'Geeks for Geeks' }

Explanation:

In the above example, the fetchData function simulates an asynchronous operation (like making an API request) by using setTimeout. After the simulated delay, it invokes the callback function callback with the fetched data. The callback function handleData receives the fetched data and performs some operations with it. In this case, it logs the data to the console. When we call the fetchData function, we pass the URL, and the callback function handleData.

Promises

Promises in JavaScript are used to manage asynchronous operations more effectively compared to traditional callbacks. They represent a value that will be available in the future. When the value is ready, the promise is either fulfilled or rejected, triggering the corresponding code execution. This helps avoid “callback hell” and makes code more readable and maintainable. Promises are created using the `Promise` constructor, which accepts a function defining the operation. Once the operation completes, the promise is either resolved or rejected. Promises support chaining with the `.then()` method, facilitating the creation of complex workflows.

Steps to define a promise:

  • Create a Promise using the Promise constructor, which takes a function with resolve and rejects parameters.
  • Use the resolve function to fulfill the promise with a value or the reject function to reject the promise with an error.
  • Handle the fulfilled and rejected states of the promise using the .then() and .catch() methods.

Syntax:

function functionName(){
return new Promise(function(resolve, reject){
// Operation
});
}

functionName()
.then(result)
.catch(error);

Example 1: Implementation to show the use of promises with an example.

JavaScript
// Function that divides two numbers
// and returns a promise
function divide(a, b) {
    return new Promise(function (resolve, reject) {
        if (b === 0) {

            // Reject the promise with an
            // error if b is zero
            reject(new Error("Cannot divide by zero"));
        } else {
            // Resolve the promise with 
            // the result of the division
            resolve(a / b);
        }
    });
}

// Call the divide function with arguments 10 and 2
divide(10, 2)
    .then(function (result) {

        // Log "Promise" to the console
        console.log("Promise"); 

        // Log the result with a prefix "Ans: "
        console.log("Ans: " + result); 
    })
    .catch(function (error) {

        // Log any errors that occurred
        // during the division
        console.log(error); 
    });

Output
Promise
Ans: 5

Exaplanation:

In the above example, the divide function returns a promise that resolves to the result of dividing a by b. If b is zero, the promise is rejected with an error message. Then the promise is used in the then method to handle the fulfillment of the promise (i.e. when the result is available) and the catch method is used to handle the rejection of the promise (i.e. when an error occurs). In this case, the result of the division is logged into the console.

Example 2: Implementation to show the use of promises with an example.

JavaScript
// Function that performs an asynchronous
// operation and returns a promise
function getUser(id) {
    return new Promise(function (resolve, reject) {

        // Simulating an asynchronous 
        // operation (e.g., fetching
        // user data from a database)
        setTimeout(() => {
            const users = {
                1: { id: 73, name: "Geek" },
                2: { id: 74, name: "Geeks for Geeks" },
                3: { id: 75, name: "M." }
            };

            if (users[id]) {

                // Resolve the promise with
                // the user data if found
                resolve(users[id]);
            } else {

                // Reject the promise with an
                // error if user not found
                reject(new Error("User not found"));
            }
        }, 2000); // Simulating a 2-second delay
    });
}

// Call the getUser function with user ID 2 and 
//handle the promise using then and catch
getUser(2)
    .then(function (user) {

        // Log the retrieved user data to
        // the console
        console.log("User:", user);
    })
    .catch(function (error) {

        // Log any errors that occurred during
        // the asynchronous operation
        console.log("Error:", error);
    });

Output
User: { id: 74, name: 'Geeks for Geeks' }

Explanation:

  • In the above example, the getUser function takes a user ID as an argument and returns a promise. Inside the promise, we simulate an asynchronous operation by using setTimeout. After the simulated delay, we check if the user with the given ID exists in our sample user data.
  • If the user is found, we resolve the promise with the user data. If the user is not found, we reject the promise with an error.
  • We call the getUser function with user ID 2 and handle the promise using the then and catch methods. The then method is used to handle the resolved promise and receive the user data. The catch method is used to handle any errors that occurred during the asynchronous operation.
  • When the promise is resolved, we log the retrieved user data to the console. If there are any errors, we log the error message instead.

Async/await

async/await in Node.js simplifies asynchronous code to look and behave more like synchronous code, enhancing readability and maintainability. Marking a function with async makes it return a promise, while await pauses execution within the async function until the awaited promise resolves or rejects, returning its value. This approach avoids the complexity of nested callbacks and makes asynchronous code easier to manage compared to traditional callbacks or promises.

Some steps to use an Async/await

  • Define an asynchronous function using the async keyword before the function declaration.
  • Within the asynchronous function, use the await keyword to pause the function until a promise is fulfilled or rejected.
  • When using await, make sure the expression following it is a promise. If it is not a promise, it will be implicitly converted to a resolved promise with the value of the expression.
  • Use try-catch block to handle any errors that may be thrown by the awaited promise.
  • Call the asynchronous function and handle the resolved or rejected promise using .then() and .catch() as you would with regular promises.

Syntax:

async function functionName(){
await wait(ms);
}

functionName().catch(() => {});

Example 1: Implementation to show the use of async await with an example.

JavaScript
// Function that returns a promise that
// resolves after a specified time
function wait(time) {
    return new Promise(
        resolve => setTimeout(resolve, time));
}

// Async function that demonstrates
// the usage of async/await
async function example() {

    // Log a message indicating the 
    // start of the async function
    console.log("Async/await Starting...");

    // Wait for 1 second
    await wait(1000); 

    // Log a message after 1 second has passed
    console.log("One second has passed!");

    // Wait for 2 seconds
    await wait(2000); 

    // Log a message after 2 more
    // seconds have passed
    console.log("Two more seconds have passed!");
}

// Call the async function to start
// the execution
example(); 

Output
Async/await Starting...
One second has passed!
Two more seconds have passed!

Explanation:

In the above example, we define a wait function that returns a promise that resolves after a given amount of time (in milliseconds). We then define an example async function that uses await to pause execution until the promises returned by the wait function have resolved. When the example is called, it logs a message, waits for one second, logs another message, waits for two more seconds, and logs a final message. Because we are using async and await, we don’t have to use callbacks or chain promises together, which can make our code more readable and easier to reason about.

Example 2: Implementation to show the use of async await with an example.

JavaScript
// Async function that adds two
// numbers and returns the sum
async function add(a, b) {

    // Add the numbers a and b
    const sum = a + b; 
    return sum; // Return the sum
}

// Async function that demonstrates
// the usage of async/await
async function run() {

    // Await the result of adding 2 and 3
    const result = await add(2, 3);

    // Log a message indicating the 
    // use of async/await
    console.log("Async/await");

    // Log the sum with a prefix "Sum is: "
    console.log("Sum is: " + result);
}

// Call the async function to
// start the execution
run(); 

Output
Async/await
Sum is: 5

Explanation:

In the above example, the add function is marked as async, indicating that it returns a promise. The run function is also marked as async, and it uses the await keyword to wait for the promise returned by add to resolve before continuing execution.

Overall, in Node.js, the control flow of function calls can be managed using callbacks, promises, or async/await, depending on the specific needs and requirements of the program. These mechanisms provide a powerful and flexible way of managing the flow of function calls, allowing Node.js developers to build highly efficient and scalable applications.



Similar Reads

Troubleshooting HTML Form Controls: Resolving the 'Invalid Form Control' Error
HTML form is used to collect input from users, where form acts like a container for different types of input like text, number, or checkboxes. Form is an important part in web development, which allows users to input and submit data, making the application interactive. However, when working with forms you may see this error "Invalid form control".
3 min read
SASS | Shadowing and Flow Control
Shadowing and Flow control are methods of changing the value of variables. These two methods are very useful when we are working with a SASS library. We will discuss it one by one. Shadowing: Local variables can be declared with the same name as Global variables. If we do that then the value of that variable in the local scope will be the value of
2 min read
D3.js Control Flow API Complete Reference
The D3 is an abbreviation of Data-Driven Documents, and D3.js is a resource JavaScript library for managing documents based on data. D3 is one of the most effective frameworks to work on data visualization. Control Flow: D3.js selection.each() FunctionD3.js selection.call() FunctionD3.js selection.empty() FunctionD3.js selection.nodes() FunctionD3.
1 min read
Which property is used to control the flow and formatting of text ?
In this article, we will discuss the property that is used to control the flow and formatting of text. The white-space property in CSS is used to control the text wrapping and white-spacing ie., this property can be used to set about the handling of the white-space inside the elements. There are several types of values in this property to use. Synt
2 min read
TypeScript Control Flow Analysis
In this article, we are going to learn about Control flow analysis in Typescript. TypeScript is a popular programming language used for building scalable and robust applications. Control flow analysis in TypeScript refers to the process by which the TypeScript compiler analyzes the flow of your code and makes inferences about the types of variables
3 min read
Node.js assert tracker.calls() Function
The tracker.calls() method is used to keep track of the number of times a function is executed. It returns a wrapper function that should be invoked at exact times. When tracker.verify() is executed, if the method has not been called precisely exact times, tracker.verify() will throw an exception. Syntax: tracker.calls([fn][, exact]) Parameters: fn
1 min read
Difference between Proxied and Proxyless Calls ?
Proxied and Proxyless calls are two different communication methods in a network or system. A proxied call involves using an intermediary or proxy server to facilitate communication between two parties. On the other hand, a proxyless call does not use a proxy server and involves direct communication between the two parties. Understanding the differ
5 min read
How does call stack handle function calls in JavaScript ?
In this article, we will see how the call stack in JavaScript handles the functions in a JavaScript program. Call StackCall Stack in any programming language is a fundamental concept responsible for the execution of function calls made in the program. While execution it also manages their order of execution. The call stack operates on the Last-In-F
2 min read
How to Use Redux Toolkit in React For Making API Calls?
In this article, we will explore how to use the Redux Toolkit in a React.js application to streamline state management and handle asynchronous operations, such as making API calls. Redux Toolkit's createAsyncThunk simplifies the creation of asynchronous action creators, allowing us to efficiently fetch data from APIs. We will configure the Redux st
4 min read
HTTP REST API Calls in ElectronJS
ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime. We already know about the importance of HTTP REST
13 min read
How to use an HTTP GET or POST for Ajax Calls?
Sending an HTTP request to the server using AJAX is the most common way of fetching data these days. It provides us with methods to send and receive data. In this article, we are going to discuss GET and POST methods. GET method: This method is used to GET or RECEIVE the data from a file, API, etc. How to use GET method?To use the GET method, first
3 min read
HTML | DOM Video controls Property
The Video controls property is used to set or return whether a video should display standard video controls or not. The <video> controls attribute are reflected by this property. The video controls included in the property are: PlayPauseVolumeFullscreen ModeSeekingCaptions/SubtitlesTrack Syntax: Returning the controls property:videoObject.con
2 min read
HTML | <audio> controls Attribute
The HTML <audio> controls attribute is used to specify the control to play audio. It is the Boolean value. This attribute is new in HTML5. The audio control attribute contains the following property: PlayPauseSeekingVolume Syntax: <audio controls> Example: C/C++ Code <!DOCTYPE html> <html> <head&gt
1 min read
HTML DOM Audio controls Property
The Audio controls property is used for setting or returning whether audio should display standard audio controls or not. The <audio> controls attribute is reflected by this property. The audio controls included in the property are: PlayPauseSeekingVolume Syntax: Return the control property:audioObject.controlsSet the control property:audioOb
1 min read
HTML | <video> controls Attribute
The HTML <video> controls Attribute is used to specify the control to play video. It is the Boolean value. This attribute is new in HTML5. The video control should include: PlayPauseVolumeFull-screen ModeSeekingCaptions/Subtitles(if available)Track(if available) Syntax: <video controls> Example: C/C++ Code <!DOCTYPE html>
1 min read
How to enable/disable all input controls inside a form element using jQuery ?
Give an HTML document containing a form element. The form element contains input elements, option elements, and button elements. The task is to enable or disable all input controls inside a form using jQuery. It can be done by using prop() method. Using prop() Method: It is used to set or return the properties and values for the selected elements.
3 min read
How to remove controls of a text canvas using Fabric.js ?
In this article, we are going to see how to remove the controls of a text canvas using FabricJS. The canvas means text written is movable and can be stretched according to requirement but we will remove the controls so that it cannot be resized or rotated but only be moved. Further, the text itself cannot be edited like a textbox.Approach: To make
2 min read
How to remove only rotation controls of a canvas-type text using Fabric.js ?
In this article, we are going to see how to remove only the rotation controls of a canvas-like text using FabricJS. The canvas means text written is movable and can be stretched according to requirement but we will remove the rotation controls so that it cannot be rotated but only be moved and resized. Further, the text itself cannot be edited like
2 min read
Mouse swipe controls in Angular 9 using HammerJS
Angular is an application design framework and development platform for creating efficient and sophisticated single-page apps. It has been changed a lot from its first release. And it keeps adding new features and modifications in its releases which is a good thing. But sometimes what we have used in the previous version stops working in the latest
4 min read
How to design form controls for mobiles using jQuery Mobile Square-UI Theme plugin ?
In this article, we will learn how to design the form controls for mobile-oriented applications using the jQuery Mobile Square-UI Theme plugin. Prerequisite: Download the pre-compiled required library files from the given link and save it in your working folder for the following implementation. Example 1: C/C++ Code <!DOCTYPE html> <html
4 min read
How to design form controls for mobiles using jQuery Mobile BootstrapTheme plugin ?
The purpose of this article is to introduce some form or user input controls for mobile web designers using the jQuery Mobile Bootstrap Theme plugin. These CSS based UI controls are very attractive and interactive and help developers build beautiful apps. To design and implement the plugin, please download the required pre-compiled files or librari
5 min read
How to add controls to an audio in HTML5 ?
In this article, we will learn how to add controls to audio in HTML5. The HTML <audio> controls attribute is used to specify the control to play audio which means it allows embedding an HTML5 music player with audio controls straight into the webpage. The current HTML5 most commonly used ogg, mp3, and wav as audio formats in the audio tag bec
2 min read
How to change the size of Form controls in Bootstrap ?
Bootstrap allows to change the size of form controls using .form-control classes. For default size .form-control is used, for smaller size we can use .form-control class along with .form-control-sm and for larger size we can use .form-control class along with .form-control-lg. Syntax : For default size:class="form-control" For small size:class="for
2 min read
Foundation CSS Forms Custom Controls
Foundation CSS is an open-source & responsive front-end framework built by ZURB foundation in September 2011, that makes it easy to design beautiful responsive websites, apps, and emails that look amazing & can be accessible to any device. It is used by many companies such as Facebook, eBay, Mozilla, Adobe, and even Disney. The framework is
3 min read
Foundation CSS Controls Complete Reference
Foundation CSS is an open-source and responsive front-end framework created by ZURB in September 2011 that makes it simple to create stunning responsive websites, apps, and emails that operate on any device. Foundation CSS Controls use to create different types of switches, buttons, sliders, and translations and control them as requirements like co
2 min read
Bootstrap 5 Form Controls Disabled
Bootstrap5 Form controls Disabled are used to create a disabled input field. To create a disabled input field, we will use the disabled boolean attribute on an input to disable the input field. It will remove any pointer event from the field. Form controls Disabled Class: There are no pre-defined classes to make the input field disabled. Form contr
2 min read
Bootstrap 5 Form controls Readonly plain text
Bootstrap 5 Form Controls Readonly plain text is used to create a readonly input field as a plaintext. To create a plain text input field, we will use .form-control-plaintext class. This class removes the styling and preserves the correct margin and padding. Form Control Readonly Attribute: readonly: This attribute is used to create a readonly inpu
2 min read
Bootstrap 5 Form controls Color
Bootstrap 5 is the newest version of Bootstrap, which is the most popular HTML, CSS, and JavaScript framework for creating responsive, mobile-first websites. It was officially released on 16 June 2020 after several months of redefining its features. Bootstrap 5 Form Control Color is used to set the color in the color type input field. To create a c
2 min read
Bootstrap 5 Form controls Datalists
Bootstrap 5 is the newest version of Bootstrap, which is the most popular HTML, CSS, and JavaScript framework for creating responsive, mobile-first websites. It was officially released on 16 June 2020 after several months of redefining its features. Bootstrap 5 Form controls datalists are used to create a group of options (group of <option> e
2 min read
Bootstrap 5 Form Controls Readonly
Bootstrap 5 Form Controls Readonly is used to create a read-only input field. To create a read-only input field, we will use readonly attribute on an input to prevent modification of the input’s value. It is useful when we want to have a fixed value for the input. Bootstrap 5 Form controls Readonly Class: There are no pre-defined classes to make th
2 min read