如果状态为“准备就绪”,我想进行一些API调用。然后只有在解决之后我才想执行一些陈述。
如果状态不是private void InletPercentage_TextBox_TextChanged(object sender, EventArgs e)
{
HandleTextBox(InletPercentage_TextBox, OutletPercentage_TextBox);
}
private void OutletPercentage_TextBox_TextChanged(object sender, EventArgs e)
{
HandleTextBox(OutletPercentage_TextBox, InletPercentage_TextBox);
}
private void HandleTextBox(TextBox me, TextBox other)
{
// if not enabled then it is not user input and just leave
if (!me.Enabled) return;
// if the user has cleared the input, reset other to accept input on both again
if (string.IsNullOrEmpty(me.Text))
{
other.Text = string.Empty;
other.Enabled = true;
return;
}
// handle user input
other.Enabled = false;
int value;
if (int.TryParse(me.Text, out value) && value >= 0 && value <= 100)
{
other.Text = (100 - value).ToString();
}
else
{
other.Text = string.Empty;
MessageBox.Show("Invalid input");
}
}
,我不想进行API调用,但仍会执行语句。
我这样做了:
ready
我知道我在这里做了一些不好的编码实践。
我可能根本不需要if(job.status === 'ready')
//Makes a promise to fetch the progress API details for ready batches only
var promise = HttpWrapper.send('/api/jobs/'+job.job_id+'/getDetails', { "operation": 'GET' });
//Once the promise is resolved, proceed with rest of the items
$q.all([promise])
.then(function(result) {
//Because for not ready batches promise object and it's response wuld be undefined
if(result[0] !== undefined){
//Create a property that would hold the progress detail for ready batches
job.pct = result[0].pct;
}
//Want to execute these lines no matter what
vm.job = job;
vm.loading = false;
。
但我无法弄清楚如何处理这种情况 - 因为最后两行会被执行
$q.all
如何有效地编写它们以便处理这两种情况?
答案 0 :(得分:0)
这应该有效,无论我建议尝试.finally
块,都要执行。您可以检查作业状态,然后调用作业详细信息功能。
if (job.status === 'ready') {
getJobDetails(job);
} else {
defaults(job);
}
function getJobDetails(job) {
return $http.get('/api/jobs/' + job.job_id + '/getDetails')
.then(function(resp) {
if (resp) {
job.pct = result[0].pct;
}
})
.catch(funcition(error) {
console.log(error);
})
.finally(function() {
vm.job = job;
vm.loading = false;
});
}
function defaults(job) {
vm.job = job;
vm.loading = false;
}