Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

react Scheduler source code's simple question

Hello I am learning the source code of react Scheduler part.
I understand the concept part to some extent, but I don’t understand the code below.

// Capture local references to native APIs, in case a polyfill overrides them.
const localSetTimeout = typeof setTimeout === 'function' ? setTimeout : null;
const localClearTimeout =
  typeof clearTimeout === 'function' ? clearTimeout : null;
const localSetImmediate =
  typeof setImmediate !== 'undefined' ? setImmediate : null; // IE and Node.js + jsdom

When I thought about it, I think I could use it after inspecting setTimeout, clearTimeout, and setImmediate itself, but I wonder why there is a code like this.

Of course, the comments say to prepare for overriding of polyfills, but I don’t understand exactly what situation you’re talking about.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Any help understanding the code would be appreciated.

>Solution :

Well let me explain to you as simply as I can. The source code we have here captures references to native APIS such as (setTimeout, clearTimeout etc..) and stores them in local variables.
This procedure is done when a polyfill library overrides these APIs but with its own implementations.

Now what is a polyfill
Its a piece of code that provides a feature that is not supported in a native way although it is required by the code. It provides some kind of compatibility for older browsers.

You could say that this code is a defensive programming technique that ensures that the code works correctly in all environments, even if a polyfill overrides native APIs.

I hope it helps

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading