<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Tobani Esan-George</title>
    <description>The latest articles on Forem by Tobani Esan-George (@tobshub).</description>
    <link>https://forem.com/tobshub</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F951437%2F2248239a-4307-4651-bed0-29fcefcd8f37.jpg</url>
      <title>Forem: Tobani Esan-George</title>
      <link>https://forem.com/tobshub</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/tobshub"/>
    <language>en</language>
    <item>
      <title>Promises, on your mark... set... Go!</title>
      <dc:creator>Tobani Esan-George</dc:creator>
      <pubDate>Thu, 01 Dec 2022 06:13:57 +0000</pubDate>
      <link>https://forem.com/tobshub/promises-on-your-mark-set-go-1ieg</link>
      <guid>https://forem.com/tobshub/promises-on-your-mark-set-go-1ieg</guid>
      <description>&lt;p&gt;More often than not, we need to use promises (async/await and so on) to fetch resources from an API or server. Unfortunately, not all APIs or servers are hosted on super fast and robust systems. &lt;/p&gt;

&lt;p&gt;So what can you do if a fetch response is an important part of loading your UI?&lt;/p&gt;

&lt;p&gt;Race your Promises!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race" rel="noopener noreferrer"&gt;Promise.race()&lt;/a&gt; is a method on the Promise object that takes an array of promises and returns the first one to finish.&lt;/p&gt;

&lt;p&gt;How is this useful for loading UIs?&lt;br&gt;
Take a look at this example using React and React-Router-DOM...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function getUserInfo(user) {
  const getUser = await fetch(`https://example.com/api/get_user/${user}`).json()
  return getUser;
}

async function fallBackUser() {
  let anon;
  setTimeout(()=&amp;gt; anon = {name: "Anonymous"}, 1000)
  return anon;
}

async function appLoader({params}) {
  const requestUser = Promise.race([getUserInfo(params.user), fallBackUser()])
  return requestUser;
}

function App() {
  const userInfo = useLoaderData()
  return (
    &amp;lt;h1&amp;gt;Username: {userInfo.name}&amp;lt;/h2&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, if the server/API doesn't send a response before 1 second(1000 milliseconds) is elapsed, requestUser takes on the value returned from the fallBackUser function, and the app is rendered.&lt;/p&gt;

&lt;p&gt;Promise.race() has many use cases and can be used to make applications faster, or avoid getting stuck. &lt;/p&gt;

&lt;p&gt;Or just use Axios :)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>promises</category>
      <category>webdev</category>
      <category>react</category>
    </item>
  </channel>
</rss>
