Skip to content Skip to sidebar Skip to footer

Js, Object's State Monitoring

Sometimes on server I have situation when it's necessary to get some data from object in the time it is being changed by another piece of code. It's important to have up to date ob

Solution 1:

You can use Promise for this case. How it has to be structured depends on your actual use-case.

A setup could look like this:

Obj1 = {};

//async part that modifies objectfunctionmodify_func() {

  //at this point you might need to check if modifying is already in progress.Obj1._modifying = newPromise(function(resolve, reject) {
    //modifying operationsdeleteObj1._modifying;
    resolve();
  });
}

functionview_func() {
  if (Obj1._modifying) {
    Obj1._modifying.then(_view_fun_internal);
  } else {
    _view_fun_internal();
  }

}

function_view_fun_internal() {
  //do some stuff on the ready object
}

But be award that this will only work well if the collecting of the data in _view_fun_internal is sync.

Post a Comment for "Js, Object's State Monitoring"