Logo
X
  • Who We Serve
    • By Role

      • CEO / Business Executives
      • CTO / IT Professionals
      • COO / Operations Head
    • By Industries

      • Healthcare
      • Digital Commerce
      • Travel and Transportation
      • Real Estate
      • Software and Technology
  • Our Technology Focus
    • Web
    • Mobile
    • Enterprise
    • Artificial Intelligence
    • Blockchain
    • DevOps
    • Internet Of Things
  • Discover Daffodil
    • About
    • Leadership
    • Corporate Social
      Responsibility
    • Partners
    • Careers
  • Resources
    • Blog

    • E-Books

    • Case Studies

    • View all resources

  • Who We Serve
    • By Role

      • CEO / Business Executives
      • CTO / IT Professionals
      • COO / Operations Head
    • By Industries

      • Healthcare
      • Digital Commerce
      • Travel and Transportation
      • Real Estate
      • Software and Technology
  • Our Technology Focus
    • Web

      Create responsive web apps that excel across all platforms

    • Mobile

      User centric mobile app development services that help you scale.

    • Enterprise

      Innovation-driven enterprise services to help you achieve more efficiency and cost savings

      • Domains
      • Artificial Intelligence
      • DevOps
      • Blockchain
      • Internet Of Things
  • Discover Daffodil
    • About
    • Leadership
    • Corporate Social Responsibilities
    • Partners
    • Careers
  • Resources
    • Blog

      Insights for building and maintaining your software projects

    • E-Books

      Our publications for the connected software ecosystem

    • Case Studies

      The impact that we have created for our clients

    • View all resources
daffodil-logo
Get in Touch
  • What We Do
    • Product Engineering

    • Discover & Frame Workshop
    • Software Development
    • Software Testing
    • Managed Cloud Services
    • Support & Maintenance
    • Smart Teams

    • Dedicated Teams
    • Offshore Development Centre
    • Enterprise Services

    • Technology Consulting
    • Robotic Process Automation
    • Legacy Modernization
    • Enterprise Mobility
    • ECM Solutions
  • Who We Serve
    • By Industry

    • Healthcare
    • Software & Technology
    • Finance
    • Banking
    • Real Estate
    • Travel & Transportation
    • Public Sector
    • Media & Entertainment
    • By Role

    • CEO / Business executives
    • CTO / IT professionals
    • COO / Operations
  • Our Expertise
    • Mobility
    • UI/UX Design
    • Blockchain
    • DevOps
    • Artificial Intelligence
    • Data Enrichment
    • Digital Transformation
    • Internet of Things
    • Digital Commerce
    • OTT Platforms
    • eLearning Solutions
    • Salesforce
    • Business Intelligence
    • Managed IT Services
    • AWS Services
    • Application Security
    • Digital Marketing
  • Case Studies
  • Discover Daffodil
    • About us
    • Partnership
    • Career & Culture
    • Case Studies
    • Leadership
    • Resources
    • Insights Blog
    • Corporate Social Responsibility
Get in Touch
blog header image.png

Software Engineering Insights

How to Avoid Duplicate API Requests?

Mar 17, 2020 5:43:00 PM

  • Tweet

duplicate API request

Duplicate API requests are common and if dealt wisely,  can help developers in creating a seamless user experience. In a scalable application, duplicate API requests can be problematic to the resources on a server, can affect the cost, and can interrupt performance.

That is why it is important to pay attention to API calls and ensure that no duplicate request is passed to the API. The later segment discusses some of the probable reasons that can lead to duplicate API request issues and ways to overcome the problem. 

Duplicate API Requests: Possible Reasons

There can be different scenarios where an API is called multiple times to get the data. For example, 

  • When a user taps on a button multiple times before it gets disabled.

  • At times, one API response causes another API request to execute. Let’s understand this with an analogy. There are several books with the same author details. As the details of the book get loaded, another requests to load the author's details is passed consequently. In this scenario, the request for multiple book details can hit the author's details API (while one is already under execution).

  • API requests on scroll events can hit an API multiple times as the scroll event triggers rapidly. 

image 1

 Same API called multiple times

Fixing Duplicate API Request Problem

While there can be several ways to fix this problem, the one that we share here leverages promise behaviour, which requires minimal changes to the codebase. Let’s understand how this solution works. 

‘Promise’ is an effective tool to handle asynchronous processes. Here is an implementation of promises to handle duplicate API requests.

class MemoPromise {

 constructor(getPromise) {

   this.cache = {};

   this.getPromise = getPromise;

   this.request = this.request.bind(this);

 }

 request({ uniqueKey, ...rest }) {

   if (!uniqueKey) {

     return Promise.reject(new Error('Unique key not passed'));

   }

   if (this.cache[uniqueKey]) {

     return this.cache[uniqueKey];

   }

   const promise = this.getPromise(rest);

   this.cache[uniqueKey] = promise

     .then((res) => {

       delete this.cache[uniqueKey];

       return res;

     })

     .catch((err) => {

       delete this.cache[uniqueKey];

       throw err;

     });

   return this.cache[uniqueKey];

 }

}

In the above example, we have created a MemoPromise class whose instantiated object can memorize the promises returned by the passed function in the constructor till the time they are not resolved or rejected. The aforementioned implementation can be executed as below: 

const memoPromise = new MemoPromise(fn);

// invocation

const { request } = memoPromise;

request({ uniqueKey: url, apiType, url, payload });

// not required now

// fn({ apiType, url, payload });

`fn` is the method under which API request is defined and it returns the request made as a promise (which is default). Instead of calling `fn` each time now we will be calling the `request` method consisted of memoPromiseObj.

image 2No Duplicate API Requests

By integrating the MemoPromise class, it is possible to effectively deal with duplicate API requests. We hope this works with you as well.

For any query or discussion, you can initiate a conversation through the comments section below. 

Topics: Web Development Software Development Mobile App Development

Team Daffodil

Written by Team Daffodil

We equip smart companies with smarter solutions. Our all-round expertise in Web, Mobile and Cloud solutions has brought about a change in some of the top businesses, setting global milestones as we go.

[fa icon="facebook-square"] [fa icon="linkedin-square"] [fa icon="twitter-square"]

Previous Post

previous_post_featured_image

Why Nodejs is the Right Choice for Enterprise Organizations?

Next Post

next_post_featured_image

10 Industries most Affected by Coronavirus (COVID-19)

Stay Ahead of the Curve with Our Weekly Tech Insights

  • Recent
  • Popular
  • Categories

Lists by Topic

  • Software Development (174)
  • Artificial Intelligence (169)
  • Mobile App Development (166)
  • Healthcare (137)
  • DevOps (80)
  • Digital Commerce (60)
  • Web Development (57)
  • CloudOps (54)
  • Digital Transformation (37)
  • Fintech (36)
  • UI/UX (29)
  • On - Demand Apps (26)
  • Software Architecture (26)
  • Internet of Things (IoT) (25)
  • Open Source (25)
  • Outsourcing (24)
  • Blockchain (21)
  • Newsroom (21)
  • Salesforce (21)
  • Technology (18)
  • Software Testing (16)
  • StartUps (16)
  • Customer Experience (14)
  • Robotic Process Automation (13)
  • Voice User Interface (13)
  • Javascript (11)
  • OTT Apps (11)
  • Business Intelligence (10)
  • Data Enrichment (10)
  • Infographic (10)
  • Big Data (9)
  • Education (9)
  • Microsoft (6)
  • Real Estate (5)
  • Banking (4)
  • Game Development (4)
  • Enterprise Mobility (3)
  • Hospitality (3)
  • eLearning (2)
  • Public Sector (1)
see all

Posts by Topic

  • Software Development (174)
  • Artificial Intelligence (169)
  • Mobile App Development (166)
  • Healthcare (137)
  • DevOps (80)
  • Digital Commerce (60)
  • Web Development (57)
  • CloudOps (54)
  • Digital Transformation (37)
  • Fintech (36)
  • UI/UX (29)
  • On - Demand Apps (26)
  • Software Architecture (26)
  • Internet of Things (IoT) (25)
  • Open Source (25)
  • Outsourcing (24)
  • Blockchain (21)
  • Newsroom (21)
  • Salesforce (21)
  • Technology (18)
  • Software Testing (16)
  • StartUps (16)
  • Customer Experience (14)
  • Robotic Process Automation (13)
  • Voice User Interface (13)
  • Javascript (11)
  • OTT Apps (11)
  • Business Intelligence (10)
  • Data Enrichment (10)
  • Infographic (10)
  • Big Data (9)
  • Education (9)
  • Microsoft (6)
  • Real Estate (5)
  • Banking (4)
  • Game Development (4)
  • Enterprise Mobility (3)
  • Hospitality (3)
  • eLearning (2)
  • Public Sector (1)
see all topics

Elevate Your Software Project, Let's Talk Now

Awards & Accolades

dj
dj
dj
dj
dj
Aws-certification-logo
microsoft-partner-2-1
microsoft-partner
google-cloud-partne
e-UI-Path-Partner-logo
partner-salesforce-reg-consulting-partner-1-1
daffodil-logo
info@daffodilsw.com
  • Home
  • About Daffodil
  • Locations
  • Privacy Policy
  • Careers

© 2025 Daffodil Unthinkable Software Corp. All Rights Reserved.

[fa icon="chevron-up"]