When to use the promise returned by ractive.set?

The ractive.set method returns a promise. Doing a simple assignment (one value or a map) and then immediately referencing the new value via ractive.get, is it a good idea to use a promise? Or is it completely unnecessary?

I avoid promises and found that I didn't need it, but maybe I'm lucky so far. Here's an example of what I mean:

ractive.set("foo", "bar");
console.log(ractive.get("foo"));   // always outputs the correct value "bar"

      

I'm worried that the set operation is asynchronous, and this will become obvious on slower machines or if I start using more complex Ractive features.

According to active documents :

[ractive.set] Returns a promise that the operation is called after the set and any transitions are complete .

Based on this, I am wondering if the promise is actually intended to work after the transition.

+3


source to share


1 answer


Based on this, I am wondering if this is really the promise of post-transition work.

Quite right. Updating the value (and the resulting DOM changes for the template) happens synchronously , the promise is for an asynchronous response to the end of transitions.



This also means that the operation set

also has a hashmap parameter for the inputs, so multiple sets will be collected in one go:

ractive.set({
    foo: 'foo',
    bar: 'bar'
}).then( () => {
   // this happens asynchronously ***after*** code execution has
   // continued below on next event cycle or after transitions complete 
});

// data and DOM have been updated as the code continues synchronously here:
console.log( ractive.get() );

      

+6


source







All Articles