Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Increasing API Reliability: Adding Timeouts to Node.js Fetch

Posted on Sep 20 When consuming external APIs and services you need to ensure they won't cause trouble to other parts of your system.When an external API is down, you can try to switch to another provider if you have a fallback.When an API is very slow, you can't be sure if the API is down or just too slow.My recommendation is to have a Timeout limit when calling any external API.The code above creates a custom error that will be thrown when a timeout happens.This higher-order function is just a race condition from a setTimeout and the fetch request.The usage with fetch is like this:We create a new Response with the status code 408 (Request Timeout) to make it easy for the product code to handle a request timeout, without needing to handle with try-catch. As your services grow, you need to keep adding more and more protection to avoid incidents.Adding a fixed timeout for all external API calls increases the reliability of your services.What else are you using to increase the reliability of your services?WooviWoovi is a Startup that enables shoppers to pay as they like. To make this possible, Woovi provides instant payment solutions for merchants to accept orders.If you want to work with us, we are hiring!Photo by Kenny Eliason on UnsplashTemplates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse Kiran Johns - Aug 31 Lasantha Sanjeewa Silva - Aug 31 Devanshu-17 - Aug 18 Muly Gottlieb - Aug 24 Once suspended, woovi will not be able to comment or publish posts until their suspension is removed. Once unsuspended, woovi will be able to comment and publish posts again. Once unpublished, all posts by woovi will become hidden and only accessible to themselves. If woovi is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Sibelius Seraphini. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag woovi: woovi consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging woovi will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Increasing API Reliability: Adding Timeouts to Node.js Fetch

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×