如果状态为“准备就绪”,我想进行一些API调用,然后一次解决后我想执行一些语句。处理有条件的角落承诺
如果状态不是ready
我不想进行API调用,但仍会执行语句。
我已经完成它是这样的:
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
。
但我无法弄清楚如何处理这种情况 - 因为最后两行会被处决
For ready batches within then only after that promise resolves, but for other batches there is no promise. So they can get executed quickly.
我怎样才能有效地写出来,这样既情况的处理方式?
您需要一个包含最后2行的else块。 –
@DeepthiS我想让他们执行if块也。 – StrugglingCoder