gpt4 book ai didi

javascript - 多次调用[promise的]解析

转载 作者:行者123 更新时间:2023-11-28 03:46:26 24 4
gpt4 key购买 nike

我有这个函数,它在异步获取数据时返回一个 promise 。正如您所看到的,该 Promise 的代码 ($watchCollection) 中有一些观察者,它们会持续观察数据并为每次更改触发 Promise 的解析函数。

我无法测试 then 是否会为每个解析函数调用触发多次。这可能吗?

this.getGridDataAndMetaData = function(viewId) {          

return new Promise(function(resolve, reject) {

dataStore.fetchView(viewId)
.then(function(viewEntity) {

var grid_groupement = viewEntity.descriptor.state ? viewEntity.descriptor.state.grid_groupement : undefined;
var dataStoreEntities = viewEntity.getEntityType().getEntities();
var dataStoreProperties = viewEntity.getEntityType().getProperties();

var gridDataAndMetaData;

$rootScope.$watchCollection(function() {
return dataStoreEntities;
},
function(newCollection, oldCollection) {

if (!angular.equals(newCollection, {}))
formatDataStoreEntities(angular.copy(newCollection), grid_groupement)
.then(function(data) {


gridDataAndMetaData = data;

if (lastNewProperties != undefined && !angular.equals(lastNewProperties, [])) {
data.viewEntity = viewEntity;
data.grid_groupement = grid_groupement;
data.properties = lastNewProperties;

resolve(data);
}
});

});

$rootScope.$watchCollection(function() {
return dataStoreProperties;
},
function(newProperties, oldProperties) {
lastNewProperties = angular.copy(newProperties);
if (gridDataAndMetaData != undefined && !angular.equals(gridDataAndMetaData.valuesIdsEntitiesTabFormat, {})) {
generateServiceTabs(newProperties, function() {
data.viewEntity = viewEntity;
data.grid_groupement = grid_groupement;
data.properties = lastNewProperties;

resolve(data);
});
}
});
}).catch(function(error) {
console.error('%c getGridDataAndMetaData :: dataStore.fetchView(viewId):: error : ' + JSON.stringify(error), 'background: red; color: white; display: block;');
reject(error);
});


});

}

最佳答案

resolve 函数是 Promise 执行器函数的参数,最多执行一次。 Promise 只能实现一次。请注意,给定的 dataStore.fetchView(viewId) 返回一个 PromisePromise 构造函数不是必需的。

关于javascript - 多次调用[promise的]解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48438194/

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