JavaScript:函数内部的执行序列

JavaScript:函数内部的执行序列,javascript,Javascript,我的视图中有一个微调器元素,它与vm.wdataloading相对应。当vm.WndatalLoading为true时,将显示微调器。我想在使用reportDataService.getStandardReportGridData加载数据时停止显示微调器。这是一种正确的方法,还是我需要以某种方式确保vm.wdataloading在数据完全加载之前不会更改为false function changeTitle(title) { /////some code function getData()

我的视图中有一个微调器元素,它与vm.wdataloading相对应。当vm.WndatalLoading为true时,将显示微调器。我想在使用reportDataService.getStandardReportGridData加载数据时停止显示微调器。这是一种正确的方法,还是我需要以某种方式确保vm.wdataloading在数据完全加载之前不会更改为false

function changeTitle(title) {
 /////some code
 function getData() {
  vm.wuDataLoading = true;

  reportDataService.getStandardReportGridData(projectId, webreportIdWuSet).then(function(data3) {
   vm.setRowWu01 = (formatNumber(data3.Values[0][1]));
   vm.setRowWu02 = (formatNumber(data3.Values[0][2]));
  });

////some code
  vm.wuDataLoading = false;
 });

getData();

};

大概
reportDataService.getStandardReportGridData
返回承诺是有原因的:它是异步的。因此,在异步工作完成之前(例如,在
然后
回调中),您自然不希望清除加载标志。大概是这样的:

function getData() {
    vm.wuDataLoading = true;

    reportDataService.getStandardReportGridData(projectId, webreportIdWuSet).then(function(data3) {
        vm.setRowWu01 = (formatNumber(data3.Values[0][1]));
        vm.setRowWu02 = (formatNumber(data3.Values[0][2]));
    })
    // "finally"
    .catch(e => {
        // Do something about the fact the promise was rejected, but don't throw
        // and don't return a rejected promise
    })
    .then(() => {
        // Because our `catch` above converts the rejection to resolution, this gets
        // called regardless of whether the original promise rejects or resolves
        vm.wuDataLoading = false;
    });

    ////some code <== This may or may not need to be in a callback above, depending on whether
    // you want it to run before or after the work is complete
});

只是将
vm.wdataloading=false行内的
然后
回调???@NiettheDarkAbsol:如果承诺拒绝?
function getData() {
    // Increment the counter, where >0 means "loading"
    ++vm.wuDataLoading;

    reportDataService.getStandardReportGridData(projectId, webreportIdWuSet).then(function(data3) {
        vm.setRowWu01 = (formatNumber(data3.Values[0][1]));
        vm.setRowWu02 = (formatNumber(data3.Values[0][2]));
    })
    // "finally"
    .catch(e => {
        // Do something about the fact the promise was rejected, but don't throw
        // and don't return a rejected promise
    })
    .then(() => {
        // Because our `catch` above converts the rejection to resolution, this gets
        // called regardless of whether the original promise rejects or resolves
        // Decrement the counter again
        --vm.wuDataLoading;
    });

    ////some code <== This may or may not need to be in a callback above, depending on whether
    // you want it to run before or after the work is complete
});
function getData() {
    function cleanup() {
        // Decrement the counter
        --vm.wuDataLoading;
    }

    // Increment the counter, where >0 means "loading"
    ++vm.wuDataLoading;

    return reportDataService.getStandardReportGridData(projectId, webreportIdWuSet).then(function(data3) {
        vm.setRowWu01 = (formatNumber(data3.Values[0][1]));
        vm.setRowWu02 = (formatNumber(data3.Values[0][2]));
        cleanup();
    })
    .catch(e => {
        cleanup();
        throw e;
    });
});