Promise.all() return 一个意外的值
Promise.all() return an unexpected value
我真的很困惑我的代码,这是一个 angularjs 服务。我尝试使用 Promise.all 连接作为服务一部分的两个承诺,并将结果发送到我的控制器。
问题是 Promise.all 编辑的对象 return 是由两个相同的数组组成的。
我的代码在这里,只是为了更清楚:
batchModule.service('BatchService', ['$http', '$q', '$log', 'MessageboxService', function ($http, $q, $log, MessageboxService) {
let deferred = $q.defer();
this.loadAll = () => {
promise1 = () => {
$http.get(_restApiPath + "/batch/?processtype=IMPORT_CUSTOMER")
// loadLastFiveBatch('IMPORT_CUSTOMER')
.then(function (response) {
deferred.resolve(response.data);
// datas.push(response1);
// console.log(datas);
}, function (error) {
deferred.reject(error);
$log.error(error);
});
return deferred.promise;
};
promise2 = () => {
$http.get(_restApiPath + "/batch/?processtype=IMPORT_LAB_MARGIN")
// loadLastFiveBatch('IMPORT_LAB_MARGIN')
.then(function (response) {
deferred.resolve(response.data);
// datas.push(response2);
// console.log(datas);
}, function (error) {
deferred.reject(error);
$log.error(error);
});
return deferred.promise;
};
Promise.all([promise1(), promise2()])
.then(values => {
console.log(values);
});
};
}]);
console.log(values) return 一个由 IMPORT_CUSTOMER 请求编辑的 2 个相等数组 return 组成的对象,当 Promise.all 正是 return 由 IMPORT_MARGIN 请求编辑的承诺。
今天我一直在研究它几个小时,但我找不到任何解决方案。
我希望我足够清楚,我的英语不是很好。
感谢您的回答:-)
你的两个承诺实际上 return 相同的 $q.defer();
承诺。所以你需要放弃延迟的反模式(使用 $q.defer()
而不是仅仅 return $http.get(..);
),问题应该会自行解决。
因此将您的代码更改为:
batchModule.service('BatchService', ['$http', '$q', '$log', 'MessageboxService', function ($http, $q, $log, MessageboxService) {
this.loadAll = () => {
promise1 = () => {
return $http.get(_restApiPath + "/batch/?processtype=IMPORT_CUSTOMER")
.then(response => response.data, error => {
$log.error(error);
return $q.reject(error);
});
};
promise2 = () => {
return $http.get(_restApiPath + "/batch/?processtype=IMPORT_MARGIN")
.then(response => response.data, error => {
$log.error(error);
return $q.reject(error);
});
};
$q.all([promise1, promise2])
.then(values => {
console.log(values);
});
};
}]);
问题是因为你对所有的承诺使用了单一的延迟。因此,该引用得到解决,它将为您的 promise 调用的更多实例解决。因此,宁愿为每个请求使用不同的延迟。我仍然不喜欢使用 deferred anti-pattern。而是通过 $http.get
方法使用 promise return 中的构建。
此外,您在函数末尾有 Promise.all
,这不会 运行 angular 摘要循环。最后,如果您要更新此函数的任何绑定,则不会更新 UI 上的绑定。考虑将其链接起来使用 $q.all
this.loadAll = () => {
promise1 = () => {
return $http.get(_restApiPath + "/batch/?processtype=IMPORT_CUSTOMER")
.then(function (response) {
return response.data;
}, function (error) {
$log.error(error);
return $q.reject(error);
}
);
};
promise2 = () => {
$http.get(_restApiPath + "/batch/?processtype=IMPORT_LAB_MARGIN")
// loadLastFiveBatch('IMPORT_LAB_MARGIN')
.then(function (response) {
return response.data;
}, function (error) {
$log.error(error);
return $q.reject(error);
}
);
};
//return is needed if any function is trying to chain `loadAll` function.
return $q.all([promise1(), promise2()])
.then(values => {
console.log("promise1", values[0]);
console.log("promise2", values[1]);
});
};
我真的很困惑我的代码,这是一个 angularjs 服务。我尝试使用 Promise.all 连接作为服务一部分的两个承诺,并将结果发送到我的控制器。 问题是 Promise.all 编辑的对象 return 是由两个相同的数组组成的。 我的代码在这里,只是为了更清楚:
batchModule.service('BatchService', ['$http', '$q', '$log', 'MessageboxService', function ($http, $q, $log, MessageboxService) {
let deferred = $q.defer();
this.loadAll = () => {
promise1 = () => {
$http.get(_restApiPath + "/batch/?processtype=IMPORT_CUSTOMER")
// loadLastFiveBatch('IMPORT_CUSTOMER')
.then(function (response) {
deferred.resolve(response.data);
// datas.push(response1);
// console.log(datas);
}, function (error) {
deferred.reject(error);
$log.error(error);
});
return deferred.promise;
};
promise2 = () => {
$http.get(_restApiPath + "/batch/?processtype=IMPORT_LAB_MARGIN")
// loadLastFiveBatch('IMPORT_LAB_MARGIN')
.then(function (response) {
deferred.resolve(response.data);
// datas.push(response2);
// console.log(datas);
}, function (error) {
deferred.reject(error);
$log.error(error);
});
return deferred.promise;
};
Promise.all([promise1(), promise2()])
.then(values => {
console.log(values);
});
};
}]);
console.log(values) return 一个由 IMPORT_CUSTOMER 请求编辑的 2 个相等数组 return 组成的对象,当 Promise.all 正是 return 由 IMPORT_MARGIN 请求编辑的承诺。 今天我一直在研究它几个小时,但我找不到任何解决方案。 我希望我足够清楚,我的英语不是很好。 感谢您的回答:-)
你的两个承诺实际上 return 相同的 $q.defer();
承诺。所以你需要放弃延迟的反模式(使用 $q.defer()
而不是仅仅 return $http.get(..);
),问题应该会自行解决。
因此将您的代码更改为:
batchModule.service('BatchService', ['$http', '$q', '$log', 'MessageboxService', function ($http, $q, $log, MessageboxService) {
this.loadAll = () => {
promise1 = () => {
return $http.get(_restApiPath + "/batch/?processtype=IMPORT_CUSTOMER")
.then(response => response.data, error => {
$log.error(error);
return $q.reject(error);
});
};
promise2 = () => {
return $http.get(_restApiPath + "/batch/?processtype=IMPORT_MARGIN")
.then(response => response.data, error => {
$log.error(error);
return $q.reject(error);
});
};
$q.all([promise1, promise2])
.then(values => {
console.log(values);
});
};
}]);
问题是因为你对所有的承诺使用了单一的延迟。因此,该引用得到解决,它将为您的 promise 调用的更多实例解决。因此,宁愿为每个请求使用不同的延迟。我仍然不喜欢使用 deferred anti-pattern。而是通过 $http.get
方法使用 promise return 中的构建。
此外,您在函数末尾有 Promise.all
,这不会 运行 angular 摘要循环。最后,如果您要更新此函数的任何绑定,则不会更新 UI 上的绑定。考虑将其链接起来使用 $q.all
this.loadAll = () => {
promise1 = () => {
return $http.get(_restApiPath + "/batch/?processtype=IMPORT_CUSTOMER")
.then(function (response) {
return response.data;
}, function (error) {
$log.error(error);
return $q.reject(error);
}
);
};
promise2 = () => {
$http.get(_restApiPath + "/batch/?processtype=IMPORT_LAB_MARGIN")
// loadLastFiveBatch('IMPORT_LAB_MARGIN')
.then(function (response) {
return response.data;
}, function (error) {
$log.error(error);
return $q.reject(error);
}
);
};
//return is needed if any function is trying to chain `loadAll` function.
return $q.all([promise1(), promise2()])
.then(values => {
console.log("promise1", values[0]);
console.log("promise2", values[1]);
});
};