gpt4 book ai didi

javascript - 如何在Promise中执行resolve或reject后执行一些代码

转载 作者:行者123 更新时间:2023-11-28 05:57:59 27 4
gpt4 key购买 nike

我有一个可能在 api 调用后解析或拒绝的 promise ,我想在解析或拒绝回调中执行一些代码,我想在这些回调完成后执行一些其他代码。如何实现。

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();
});

最佳答案

您可以将 then 链接在一起。如果任何 then 抛出错误,它将进入 catch。

     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();
});

关于javascript - 如何在Promise中执行resolve或reject后执行一些代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37491476/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com