JavaScript synchronization options

前端 未结 6 933
庸人自扰
庸人自扰 2021-02-04 13:44

I was wondering whenever exists a solution to perform synchronization in JavaScript code. For example I have the following case: I\'m trying to cache some response values from A

6条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-04 13:52

    I know it is way after but thought I would just give an improvement. Artem Barger worked out his own solution which uses setTimeout if the cached value is "updating" ... Instead just tag the function onto a linked queue against the cached value so you have an object which holds the actual cached data and the queue of functions to call with the cached data once the data is returned.

    So, if it is "updating" just add your function to the queue which is linked against the item data and when the data returns...set the data and then iterate over the queue of functions (callbacks) and call them with the returned data.

    if you wanted to get technical you could set a specific QOS timeout as you go through the queue, set a stop watch at the beginning of the iteration and if you reach a specific time then call a setTimeout to go back into the queue to continue the iteration...I hope you got that :) - It basically saves X amount of setTimout calls one only one is needed.

    View Artem Bargers code above and you should get the gist of what I propose.

提交回复
热议问题