繁体   English   中英

在继续之前等待一个 function 完成的最佳方法是什么?

[英]what the best way to wait for one function to finish before continuing?

when i try to create a new record from JS to CRM with the function XrmServiceToolkit.Soap.Create its take a lot of time to finish.. so the JS continuing to execute the other function, in the function RecalculateSurface i retreive the value of the由第一个 function 创建的记录,所以我无法获得记录的值,因为它尚未保存。

function save()

{
//some code
delivery.attributes["eoz_unit"] = Unit;
delivery.attributes["description"] = quoteDetail.Description;
delivery.attributes["quoteid"] = { id: quoteId, logicalName: "quote", type: "EntityReference" };
 XrmServiceToolkit.Soap.Create(delivery);   //function 1
RecalculateSurface();
}
function RecalculateSurface()
{
// code to retreive the record created in function 1  

}

任何想法让 function RecalculateSurface() 等待记录的保存? function XrmServiceToolkit.Soap.Create 返回创建的记录的 id。

所以,创建 function 有一个可选的回调 function 参数...

XrmServiceToolkit.Soap.Create(businessEntity, [callback(result)]);

您应该使用该选项,而不仅仅是传入实体

function save() {
    //some code
    delivery.attributes["eoz_unit"] = Unit;
    delivery.attributes["description"] = quoteDetail.Description;
    var quoteidObj = {
        id: quoteId,
        logicalName: "quote",
        type: "EntityReference" 
    };
    delivery.attributes["quoteid"] = quoteidObj;
    // do function 1
    XrmServiceToolkit.Soap.Create(delivery, function(result){  
        RecalculateSurface();
    });   
}

function RecalculateSurface() {
    // code to retrieve the record created in function 1  
}

这应该会导致 XrmServiceToolkit.Soap.Create 调用异步操作,并且仅在完成时调用 RecalculateSurface()。

如果您想扩展解决方案以使用 Promise,那么您可以执行以下操作:

function save() {
    //some code
    delivery.attributes["eoz_unit"] = Unit;
    delivery.attributes["description"] = quoteDetail.Description;
    var quoteidObj = {
        id: quoteId,
        logicalName: "quote",
        type: "EntityReference" 
    };
    delivery.attributes["quoteid"] = quoteidObj;
    callCreate(delivery).then(function(){
        RecalculateSurface()
    });
}

function callCreate(delivery){
    return new Promise(function(resolve,reject) {
        XrmServiceToolkit.Soap.Create(delivery, function(result){
            resolve();
        });
    });
}

function RecalculateSurface() {
    // code to retrieve the record created in function 1  
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM