Someone mentioned that calling cascaded async functions don't work. However I'm trying a simple test with a for-next loop and getting unpredictable results. The console.log always shows the last item but all the items get created. Is there something wrong with the way I'm reporting the ID?
<script type="text/javascript">
function btnSubmit_OnClientClick() {
for (i = 0; i < 3; i++) {
create(i);
}
console.log('completed main code');
}
function create(i) {
var clientContext = new SP.ClientContext.get_current();
var oList = clientContext.get_web().get_lists().getByTitle('TestPromises');
var itemCreateInfo = new SP.ListItemCreationInformation();
this.oListItem = oList.addItem(itemCreateInfo);
oListItem.set_item('Title', 'Item '+ i );
oListItem.update();
clientContext.load(oListItem);
clientContext.executeQueryAsync(Function.createDelegate(this, this.SaveEmployeeSuccess), Function.createDelegate(this, this.SaveEmployeeFailure));
}
function SaveEmployeeSuccess() {
console.log('item created'+oListItem.get_id());
}
function SaveEmployeeFailure() {
}
</script>