函数2使用函数1返回,而函数3使用两个返回,如何干净利落? 函数3只返回未定义的值right nom。
以下是我的3个功能。
功能1
$scope.nombreCompetencesATraiter = function(){
var nbr = 0;
notesService.getNotesByCollaborateurId($scope.idCollaborateurSelectionne).then(function(data){
$scope.myNotes = data;
angular.forEach($scope.myNotes, function(valueNote, keyNote) {
if ((valueNote.status === "EN_ATTENTE_DE_VALIDATION") || (valueNote.status === "EN_ATTENTE_DE_SUPPRESSION")){
nbr++;
}
})
console.log(nbr);
return nbr;
})
}
功能2
$scope.affinerConfigBoutonsCompetences = function() {
nbCompetenceATraiter = $scope.nombreCompetencesATraiter();
if (nbCompetenceATraiter == 0){
$scope.radioModelFilterToutesLesCompetences = true;
$scope.radioModelFilterCompetencesAValider = false;
}
else{
$scope.radioModelFilterCompetencesAValider = true;
$scope.radioModelFilterToutesLesCompetences = false;
}
}
功能3
$scope.affinerConfigBoutonsCompetencesThen = function(){
$q.all([$scope.nombreCompetencesATraiter(), $scope.affinerConfigBoutonsCompetences()]).then(function(value) {
console.log(value[0]);
console.log(value[1]);
})
}
非常感谢。
答案 0 :(得分:0)
功能1 在调用noteService
$scope.nombreCompetencesATraiter = function() {
var nbr = 0;
return notesService.getNotesByCollaborateurId($scope.idCollaborateurSelectionne)
.then(function(data) {
$scope.myNotes = data;
angular.forEach($scope.myNotes, function(valueNote, keyNote) {
if ((valueNote.status === "EN_ATTENTE_DE_VALIDATION") || (valueNote.status === "EN_ATTENTE_DE_SUPPRESSION")) {
nbr++;
}
})
console.log(nbr);
return nbr;
})
}
功能2
$scope.affinerConfigBoutonsCompetences = function() {
return $scope.nombreCompetencesATraiter().then(function (value) {
nbCompetenceATraiter = value;
if (nbCompetenceATraiter == 0) {
$scope.radioModelFilterToutesLesCompetences = true;
$scope.radioModelFilterCompetencesAValider = false;
} else {
$scope.radioModelFilterCompetencesAValider = true;
$scope.radioModelFilterToutesLesCompetences = false;
}
return nbCompetenceATraiter // value you want to return
});
}
功能3
$scope.affinerConfigBoutonsCompetencesThen = function(){
$q.all([$scope.nombreCompetencesATraiter(), $scope.affinerConfigBoutonsCompetences()]).then(function(value) {
console.log(value[0]);
console.log(value[1]);
})
}
现在让它如何运作!
答案 1 :(得分:0)
你有一些Luillyfe提到的错误,一般来说这是解决这种依赖的最简洁方法。
var f1 = () => new Promise( resolve => resolve('value1'));
var f2 = ( value1 ) => new Promise( resolve => resolve('value2'));
var f3 = ( value1, value2 ) => new Promise( resolve => resolve('done'));
f1()
.then( value1 => Promise.all([ value1, f2( value1 ) ]) )
.then( ([ value1, value2 ]) => f3( value1, value2 ))
将Promise.all
与非Promise值一起使用非常方便,它会直接将它们传递给then
。