在Promise中执行解析或拒绝后如何执​​行某些代码

时间:2016-05-27 20:05:57

标签: javascript promise

我承诺在api调用后可以解析或拒绝,我想在解析或拒绝回调中执行一些代码,我想在完成这些回调之后执行其他一些代码。如何实现它。

ApiHelper.myPromise.call(this, 'Event').then(function(response) {
            ToolsHelper.DynamicAttributes.setDAMetaDataCache.call($this, 'Event', response, '#addEventDynamicAttribute');
            ApiHelper.ApiResponseHandler.hideSpinner();

            var eventEditFormView = new EventEditFormView({'parentObj': $this.parentObj, 'method': 'add'});
            eventEditFormView.context['dynamicAttributeMetaData'] = $this.context['dynamicAttributeMetaData'];
            eventEditFormView.context['newAccordions'] = $this.context['newAccordions'];
            $this.$('#event-edit').append(eventEditFormView.$el);
            eventEditFormView.render();
            eventEditFormView.delegateEvents();
        }).catch(function(response) {console.log('err', response);
            ApiHelper.ApiResponseHandler.logError(response, 'event_base_view', ' setDynamicAttributes', 'catalog/metadata/v3/dynamicattributes', 'v3');
            ApiHelper.ApiResponseHandler.hideSpinner();
        });

1 个答案:

答案 0 :(得分:0)

你可以把你们联系在一起。如果任何一个thens抛出错误,它将进入捕获。

     ApiHelper.myPromise.call(this, 'Event')
     .then(function(response){
         ApiHelper.ApiResponseHandler.hideSpinner();
         return response;
     }
     .then(function(response) {
     ToolsHelper.DynamicAttributes.setDAMetaDataCache.call($this, 'Event', response, '#addEventDynamicAttribute');
        //ApiHelper.ApiResponseHandler.hideSpinner();

        var eventEditFormView = new EventEditFormView({'parentObj': $this.parentObj, 'method': 'add'});
        eventEditFormView.context['dynamicAttributeMetaData'] = $this.context['dynamicAttributeMetaData'];
        eventEditFormView.context['newAccordions'] = $this.context['newAccordions'];
        $this.$('#event-edit').append(eventEditFormView.$el);
        eventEditFormView.render();
        eventEditFormView.delegateEvents();
    }).catch(function(response) {console.log('err', response);
        ApiHelper.ApiResponseHandler.logError(response, 'event_base_view', ' setDynamicAttributes', 'catalog/metadata/v3/dynamicattributes', 'v3');
        //ApiHelper.ApiResponseHandler.hideSpinner();
    });