<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sami Al Subhi on Medium]]></title>
        <description><![CDATA[Stories by Sami Al Subhi on Medium]]></description>
        <link>https://medium.com/@sami_33286?source=rss-9ab07ce00b2a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*HoVLW5eiTsCf841lksXNsQ.jpeg</url>
            <title>Stories by Sami Al Subhi on Medium</title>
            <link>https://medium.com/@sami_33286?source=rss-9ab07ce00b2a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 18:40:07 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sami_33286/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Locking solution for Axios requests]]></title>
            <link>https://medium.com/@sami_33286/locking-solution-for-axios-requests-255d196d3a2d?source=rss-9ab07ce00b2a------2</link>
            <guid isPermaLink="false">https://medium.com/p/255d196d3a2d</guid>
            <category><![CDATA[authentication]]></category>
            <category><![CDATA[api]]></category>
            <category><![CDATA[axios-interceptor]]></category>
            <category><![CDATA[api-development]]></category>
            <category><![CDATA[axios]]></category>
            <dc:creator><![CDATA[Sami Al Subhi]]></dc:creator>
            <pubDate>Tue, 17 May 2022 09:55:35 GMT</pubDate>
            <atom:updated>2022-05-18T03:18:04.462Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*lvIdvdX1cj2JqFMDk7O_Ww.jpeg" /><figcaption><a href="https://unsplash.com/@jonnyrogers">Jonny Rogers</a></figcaption></figure><p>So why would you want to lock requests? this can be truly useful in the case of having authentication in place. For example, if a request is rejected by the server because access token has expired then you want to lock any upcoming requests until access token is refreshed.</p><p>Let us start this by creating the locking feature which is in the form of a class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0aa278e04785b1177ecf524f702f8e25/href">https://medium.com/media/0aa278e04785b1177ecf524f702f8e25/href</a></iframe><p>We will come back to this class and break it down. All you need to know now is that we create an instance of this class and that is what we are using everywhere in our project.</p><p>Next let us create an interceptor that utilizes this class:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/56b4ae3e9bb924c25ec3fc3041df34c0/href">https://medium.com/media/56b4ae3e9bb924c25ec3fc3041df34c0/href</a></iframe><p>Here, firstly, we create an instance of Axios and then attached a request intercepter. Any request we initiate will first pass through this interceptor. In this interceptor we check first if the lock is enabled. if it is enabled, the request will be on hold by waiting for axiosLock.waitForUnlock().</p><p>Now let’s go back to the axiosLock instance and go through the mechanism of locking. if locked is true then requests will be waiting for axiosLock.waitForUnlock(). This method will create a Promise that can be waited for until it is resolved or reaches a timeout. Each time this method is waited for, we take the resolve function out of each Promise created and store it in an array of functions named PromiseResolvers. Once axiosLock is unlocked then these resolve functions are all executed and then each request that is waiting for axiosLock.waitForUnlock() will be able to get passed this and proceeds with the request.</p><p>Now let us implement this locking feature. We can utilize a response interceptor to check if a request is rejected because of authorization issue and then retry the request after refreshing the token.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7ce595ee52a4e7442f601b61284ab92b/href">https://medium.com/media/7ce595ee52a4e7442f601b61284ab92b/href</a></iframe><p>Here if a request fails, then it will go through this response interceptor. We first check that the request is not deliberately cancelled by us (this can be removed because the following statement is enough) and also check that it has error response status code of 401. Then this means that token can be invalid. In that case we proceed to refreshing the token but before that we do we need to check if axiosLock.locked is true (maybe another request already locked it at this point). If not then we go on and we lock axiosLock using the method axiosLock.lock() then we refresh the token and then unlock axiosLock. After that we can repeat the request with the update authentication headers. Also any other requests which are on hold because of this will be released. Notice that we must utilize new instances of Axios when refreshing tokens and repeating a failed request because you do not want those requests to have possess interceptors.</p><p>That’s all. I hope this article is useful to you. Please comment if you have any questions. like and share if you like it :).</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=255d196d3a2d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>