gpt4 book ai didi

javascript - Promise.all 与 m.sync

转载 作者:行者123 更新时间:2023-11-27 23:40:08 24 4
gpt4 key购买 nike

这两个函数有什么区别吗?

我最近在 Controller 中使用 Promise.all 时发现了一个问题。 View 将在 Promise.all 完成之前渲染,并且我会在 View 内得到空变量。

因此,如果我使用一个 m.request 来访问单个 api,那么 View 将等待它完成。如果我使用许多包裹在 Promise.all 中的 m.request 那么它不会等待!我做错了什么吗?

这是正确的行为吗? m.sync 的行为是否会有所不同,它似乎与 Promise.all 具有相同的签名?

谢谢。

编辑

具有相关位的代码

// Does two requests and wraps them in Promise.all
Table.show = (id, load) => {
var header = m.request({
method: "GET",
url: cfg.apiurl(`/tables/${id}`),
});
var body = m.request({
method: "POST",
url: cfg.apiurl(`/tables/${id}`),
data: {
data: load
}
});
return Promise.all([header, body]);
};

// The component
var ShowTable = {
controller, view
};

// Controller function
function controller() {
this.header = m.prop({});
this.records = m.prop([]);
this.pages = m.prop(0);

var load = {
filter: {},
paging: {
number: 1,
size: 10
}
};

var showTable = () => {
Table.show(m.route.param("id"), load).then((res) => {
this.header(res[0].data);
this.records(res[1].data);
this.pages(res[1].meta.pages);
}, (res) => {
popa();
});
};

showTable();

}

// View function
function view(vm) {
return m("div", [
m("p", vm.header()),
m("p", vm.records()),
m("p", vm.pages()),
]);
}

编辑2

是的,m.sync 有效。但 Promise.all 却没有。

最佳答案

快速浏览一下文档,我找到了 this page 。重绘仅针对 mithril 的内置函数完成,Promise.all 是原生 JavaScript 函数,因此不会自动重绘。您应该在 showTable 函数中使用 m.syncm.startComputation/m.endComputation。您甚至可以直接返回 [header, body] 而无需 Promise.all/m.sync,它们都是 Promise 和 mithril props,因此您可以使用/将它们分配给您的 viewmodel当它们的值被填充时,它应该触发 View 中的重绘。

不管怎样,如果还是不行,别浪费时间了,直接把m.redraw()放在Promise.all的then中即可。

关于javascript - Promise.all 与 m.sync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33748937/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com