如果狀態爲「準備就緒」,我想進行一些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