简体   繁体   中英

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 record created by the first function so i can't get the value of record because its not saved yet.

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  

}

any idea to make the function RecalculateSurface() wait the save of the record? the function XrmServiceToolkit.Soap.Create return the id of the record created.

So, the create function has an optional callback function parameter...

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

You should use that option instead of just passing in entity

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  
}

This should cause the XrmServiceToolkit.Soap.Create call to operate asynchronously and only call RecalculateSurface() when it completes.

If you want to expand the solution to use promises then you could do the following:

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  
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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