Generally like CSOM in JSOM there will be no ExecuteQuery means it is not synchronous it will have executeQueryAsync.
To make JSOM code work as synchronous we need to use deferred and promise objects.
Lets take a delete function which will delete an item based on its id in sharepoint list.
Button click delete
$(“#btnDelete”).click(function () {
var p = DeleteRecordById();
p.done(function (result) {
var error = result;
console.log(error);
});
p.fail(function (result) {
var error = result;
console.log(error);
});
});
Function starts
/converting asynchronous to synchronous
function DeleteRecordById() {
var d = $.Deferred();
var id = $(“#txtID”).val();clientContext = new SP.ClientContext.get_current();
oList = clientContext.get_web().get_lists().getByTitle(‘JSOMFields’);this.oListItem = oList.getItemById(id);
oListItem.deleteObject();
clientContext.load(oListItem);clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQueryDeleteSucceeded),
Function.createDelegate(this, this.onQueryDeleteFailed));
return d.promise();
}