何时使用ractive.set返回的承诺?

Kei*_*ith 3 ractivejs

ractive.set方法返回一个promise.当执行简单的设置操作(单个值或映射)然后通过ractive.get立即引用新值时,是否建议使用promise?或者这是完全没必要的?

我一直在逃避承诺,发现我不需要它,但也许我到目前为止幸运.这是我的意思的一个例子:

ractive.set("foo", "bar");
console.log(ractive.get("foo"));   // always outputs the correct value "bar"
Run Code Online (Sandbox Code Playgroud)

我担心设置操作是异步的,这在较慢的机器上或者如果我开始使用Ractive的更高级功能时会变得明显.

根据Ractive文档:

[ractive.set]返回将在set操作和任何转换完成后调用的Promise .

基于此,我想知道这个承诺是否真的适用于转型后的工作.

mar*_*pdx 6

基于此,我想知道这个承诺是否真的适用于转型后的工作.

究竟.值update(以及每个模板生成的DOM更改)同步发生,promise用于异步响应转换结束.

这也是为什么set操作也有输入参数的哈希映射选项,因此多个集合将一次批量处理:

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() );
Run Code Online (Sandbox Code Playgroud)