gpt4 book ai didi

javascript - 如何从多个 Promise 返回 Promise

转载 作者:行者123 更新时间:2023-12-02 14:53:30 26 4
gpt4 key购买 nike

我总是在控制台中看到:

  1. 团队更新2
  2. 添加用户到项目
  3. 从项目中删除用户

但蜜蜂的顺序应该不同

var result = teamService.addOrDeleteUser(userId, newTeams, deleteTeams);
result.then(function () {
console.log("teamsUpdated2");
});

具有多个 promise 的函数:

var _addOrDeleteUser = function (userId, addToProjects, removeFromProjects) {
var deferred = $q.defer();
var promises = [];

promises.push(projectResource.addUserToProjects({ userId: userId }, addToProjects, function (result) {
console.log("addUserToProjects");
deferred.resolve();
return result;
}));

promises.push(projectResource.deleteUserFromProjects({ userId: userId }, removeFromProjects, function (result) {
console.log("deleteUserFromProjects");
deferred.resolve();
return result;
}));

return $q.all(promises);
}

最佳答案

你可以使用 promise 链 -

travelService
.getDeparture( user ) // Request #1
.then( function( departure ){
$scope.departure = departure; // Response Handler #1
return travelService.getFlight( departure.flightID ); // Request #2
})
.then( function( flight ){
$scope.flight = flight; // Response Handler #2
return weatherService.getForecast( $scope.departure.date ); // Request #3
})
.then( function( weather ){
$scope.weather = weather; // Response Handler #3
});

更多引用请参见this

关于javascript - 如何从多个 Promise 返回 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36004659/

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