javascript如何使用多个函数同步工作
How to use multiple functions to make work synchronously in javascript
这是我的功能:
$scope.saveManualResendDraft = function(todo) {
if ($scope.editMode) {
updateStartJobManual();
byeSendManualInputDirectly();
} else {
console.log('bye');
}
};
我有两个函数 updateStartJobManual() 和 byeSendManualInputDirectly().
我想完全完成第一个功能然后我需要移动到第二个,怎么办?是否可以使用承诺?我需要一些代码。
function byeSendManualInputDirectly() {
if ($window.confirm("Do you want to send this messages?"))
addProfSms();
else
console.log('no');
}
function addProfSms() {
$http.post('/api/sendprofsms', $scope.draft).then(function(response) {
swal("Good job!", "Message sended!", "success")
// $state.reload();
});
}
function updateStartJobManual() {
$http({
method: 'POST',
url: '/api/updatestartjobmanual',
data: $scope.draft
}).then(function(response) {
$scope.currentItem = response.data;
$scope.todos[$scope.currentItemIndex] = response.data;
$scope.editMode = false;
console.log('draft:', response.data);
$state.reload();
// toastr.success('Updated Successfully');
}, function(response) {
console.log('error');
});
}
您的实际代码已经同步执行 updateStartJobManual
和 byeSendManualInputDirectly
。
但是,如果您的函数正在处理 promises,两者都将以后台作业提前结束 运行。所以,让我们把承诺串起来,一个接一个地履行。
假设您的 byeSendManualInputDirectly
(和 byeSendManualInputDirectly)是这样制作的:
function byeSendManualInputDirectly(){
return $http.post('myApiAddress', {myParam: true});
}
这样,函数就是return承诺。
要连接 updateStartJobManual
和 byeSendManualInputDirectly
,您只需:
updateStartJobManual().then(function(){
byeSendManualInputDirectly()
});
我建议您阅读一些关于 promises 的文章并了解它们的工作原理(请参阅 this 有关 $q 用法的文档,promises angularjs 使用的库)
根据OP的代码编辑:
只需将 return 添加到您的函数 updateStartJobManual
,这样:
function updateStartJobManual() {
return $http({
method: 'POST',
...
}
并在您的 saveManualResendDraft
中添加一个 then() 来处理承诺:
$scope.saveManualResendDraft = function(todo) {
if ($scope.editMode)
updateStartJobManual().then(byeSendManualInputDirectly);
else
console.log('bye');
};
这是我的功能:
$scope.saveManualResendDraft = function(todo) {
if ($scope.editMode) {
updateStartJobManual();
byeSendManualInputDirectly();
} else {
console.log('bye');
}
};
我有两个函数 updateStartJobManual() 和 byeSendManualInputDirectly().
我想完全完成第一个功能然后我需要移动到第二个,怎么办?是否可以使用承诺?我需要一些代码。
function byeSendManualInputDirectly() {
if ($window.confirm("Do you want to send this messages?"))
addProfSms();
else
console.log('no');
}
function addProfSms() {
$http.post('/api/sendprofsms', $scope.draft).then(function(response) {
swal("Good job!", "Message sended!", "success")
// $state.reload();
});
}
function updateStartJobManual() {
$http({
method: 'POST',
url: '/api/updatestartjobmanual',
data: $scope.draft
}).then(function(response) {
$scope.currentItem = response.data;
$scope.todos[$scope.currentItemIndex] = response.data;
$scope.editMode = false;
console.log('draft:', response.data);
$state.reload();
// toastr.success('Updated Successfully');
}, function(response) {
console.log('error');
});
}
您的实际代码已经同步执行 updateStartJobManual
和 byeSendManualInputDirectly
。
但是,如果您的函数正在处理 promises,两者都将以后台作业提前结束 运行。所以,让我们把承诺串起来,一个接一个地履行。
假设您的 byeSendManualInputDirectly
(和 byeSendManualInputDirectly)是这样制作的:
function byeSendManualInputDirectly(){
return $http.post('myApiAddress', {myParam: true});
}
这样,函数就是return承诺。
要连接 updateStartJobManual
和 byeSendManualInputDirectly
,您只需:
updateStartJobManual().then(function(){
byeSendManualInputDirectly()
});
我建议您阅读一些关于 promises 的文章并了解它们的工作原理(请参阅 this 有关 $q 用法的文档,promises angularjs 使用的库)
根据OP的代码编辑:
只需将 return 添加到您的函数 updateStartJobManual
,这样:
function updateStartJobManual() {
return $http({
method: 'POST',
...
}
并在您的 saveManualResendDraft
中添加一个 then() 来处理承诺:
$scope.saveManualResendDraft = function(todo) {
if ($scope.editMode)
updateStartJobManual().then(byeSendManualInputDirectly);
else
console.log('bye');
};