gpt4 book ai didi

javascript - 如何使用 Promise all() 同时执行两个函数?

转载 作者:行者123 更新时间:2023-11-29 15:11:57 25 4
gpt4 key购买 nike

我有两个 ajax 函数:应该一起运行。

        getCountryList: function () {
var self = this;
App.elementService.getCountries().then(function (countriesList) {
self.countryData = countriesList;
});
},

getData: function () {
var self = this;
this.getElementTypeSchema(App.variable.SelectedCustomerName).then(function (elementTypeValues) {
self.elementTypeData = elementTypeValues;
self.render(elementTypeValues);
});
},

getCountryList() 应该在 self.render(elementTypeValues) 之前或同时执行getData() 执行。在搜索 promise 之后,我发现 Promise.all() 可以在这里工作。但我不知道该怎么做。

或者任何其他选项对我来说都可以吗?谢谢

最佳答案

Promise.all 采用一组 promises,它返回一个 Promise 本身,它在解析函数中有一组结果。

getAllData: function() {
var self = this;
Promise.all([
App.elementService.getCountries(),
self.getElementTypeSchema(App.variable.SelectedCustomerName)
]).then(function(res) {
self.countryData = res[0];
self.elementTypeData = res[1];
self.render(res[1]);
});
}

关于javascript - 如何使用 Promise all() 同时执行两个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53655134/

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