第二个函数在第一个函数之前完成

问题描述 投票:0回答:1

在以下帮助程序方法中,我尝试在将其更新为新状态之前检查当前的全向代理状态。

 ({
handleStatus: function(cmp) {
    let action = cmp.get("c.checkCurrentTimeIsWithinBusinessHours");
    action.setCallback(this, function(response) {
        let state = response.getState();
        if (state === "SUCCESS") {
            setTimeout((() => {
                let omniAPI = cmp.find("omniToolkit");
//checkStatus
                    omniAPI.getServicePresenceStatusId().then(function(result) {
                        if(result.statusApiName != null){
                            cmp.set('v.isworking', true);
                        } else {
                            cmp.set('v.isworking', false);
                        }
                    }).then(updateStatus());
                }), 1000);
            } else {
                console.log(response.getError());
            }

//update status
            function updateStatus(){
                if (response.getReturnValue().isWorking && !cmp.get('v.isworking')){
                
                cmp.set('v.isworking', true);
                omniAPI.setServicePresenceStatus({statusId: response.getReturnValue().statusId.toString()}).then(function(result) {
                }).catch(function(error) {
                    console.log(error);
                });
            
            } else if (!response.getReturnValue().isWorking && cmp.get('v.isworking')){
                cmp.set('v.isworking', false);
                let omniAPI = cmp.find("omniToolkit");
                omniAPI.logout();
            }
        }
    });
    $A.enqueueAction(action);
}

})

据我所知,async/await 在 aura 组件中不起作用,并且我不认为向第二个函数添加另一个 settimeout 是最佳实践。 所以基本上改变状态功能完成第一和检查状态 - 第二。有没有办法设置执行顺序,以便在检查状态之前完成检查状态? 预先感谢。

salesforce aura-framework
1个回答
0
投票

您应该在

.then
部分传递要调用的函数,而不是调用该函数。目前,它将在运行
updateStatus
的同时评估
omniAPI.getServicePresenceStatusId()
函数。

根据您的情况,将

.then(updateStatus())
更新为
.then(updateStatus);

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.