Xrm.WebApi calls as synchronous in Dynamics 365/ Dataverse
By default Xrm.WebApi methods are asynchronous so you may get some issues in your script logic. SO how to make this synchronous? Change the code to use async and await keywords. The sample code given here. Hope this will help.