[英]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.