The next key switch function will be called by

I need to override the following code

Here the function will be executed in the next tick

req.nextTick = typeof setTimeout !== 'undefined' ? function (fn) { setTimeout(fn, 5); } : function (fn) { fn(); }; 

with this,

 window.require.nextTick = function(fn) { fn(); }; 

Since the function will be called immediately, in this case it will not be executed at the next tick?

+5
source share
3 answers

if I change the code to whethaer second option. It will be problematic, and if so, why ???

I do not recommend making this change because it can be problematic, generally speaking. The documentation for require.nextTick (which appears immediately before the function definition) says:

Run something after the current tick of the event loop.

Calling fn synchronously violates the specification that execution should be performed "after the current tick." (See the end of my answer for a possible objection here.)

If you're wondering why this might be a problem, consider RequireJS listening for DOM events. One thing that has a function like require.nextTick is that it allows event handlers to fire . If you set require.nextTick to execute your function synchronously, you do not allow event handlers to fire. In some cases, this may cause RequireJS to stop working properly.

At this point, one could argue that the definition of nextTick is such that it can invoke its fn synchronously, because if setTimeout not defined, it invokes fn synchronously:

 req.nextTick = typeof setTimeout !== 'undefined' ? function (fn) { setTimeout(fn, 4); } : function (fn) { fn(); }; 

I think this is intended for unusual cases , and not for the queue at the mill, where the modules are loaded via HTTP requests asynchronously. In some cases, such as embedded devices that lack the JS environment provided by browsers or Node.js, the only way to run a piece of software using RequireJS is to download the optimized package that passed through r.js and enable require.js in the kit. In a case like this , calling nextTick call fn will be controversial at the same time, because by the time RequireJS is executed, all modules are already loaded.

+2
source

If you have a function like this:

 window.require.nextTick = function(fn) { fn(); }; 

each time you call window.require.nextTick() , fn() will execute immediately.

if I change the code to whethaer second option This will be problematic

No, it will not cause any problems. The only difference between the first and second code is that in the first fn() is called after 5 milliseconds, and in the second - immediately. In both examples, fn() will be called every time you call window.require.nextTick() .

+1
source

There is something fundamentally wrong with the original function. It behaves very differently if setTimeout is undefined.

Javascript runs in an event loop . Your Javascript code runs until it is executed, and other Javascript code waits for its turn in the queue. For example, an event listener for a custom event (for example, clicking a button) gets "queued" and waits for the current code to complete.

It is assumed that your nextTick function will add the function to the queue, so it will be executed later in the event loop. This is sometimes useful. For example, you may have your own event emitter and want to queue your own event.

So your nextTick function nextTick broken if setTimeout is undefined. It will perform the function now instead of adding it to the "queue". This will lead to a completely different behavior, which means errors that occur only for certain browsers.

Please correct your code to use this instead:

 window.require.nextTick = function(fn) { setTimeout(fn, 5) }; 

and ignore browsers that do not implement setTimeout . They can have a big fat mistake instead of weird subtle mistakes.

For record-only purposes, setTimeout has been working in all major browsers for the past 20 years or so. It is supported in IE4! Thus, this condition is completely unnecessary.

+1
source

Source: https://habr.com/ru/post/1239770/


All Articles