jsgrid
jsgrid copied to clipboard
Promises in onItemInserting not working
- Problem statement:
I would like to obtain server data during the onItemInserting: method using a Promise. However the new item is inserted whilst the Promise is incomplete; followed by the code (in this test case) within
setTimeout
Note:
-
The methodology to obtain server data works correctly outside the jsTable framework and employs ES6 async/await.
-
I spent several hours looking into jsgrid.js to better understand - to no avail
Demo code herewith usingsetTimeout() to illustrates the problem
onItemInserting: (args)=> {
return new Promise((resolve, reject) => {
console.log('Pre grid insert - start');
let payload = new Object();
payload.barcode = args.item.barcode;
args.cancel = false;
// Code runs OK to this point
setTimeout( () => {
args.cancel = true;
resolve();
}, 1000);
//Grid insert happens before the resolve in setTimeout
});
},
Your assistance to understand if Promises are allowed within the jsGrid framework
Google Chrome is up to date - Version 81.0.4044.138 (Official Build) (64-bit)
/*
- jsGrid v1.5.3 (http://js-grid.com)
- (c) 2016 Artem Tabalin
- Licensed under MIT (https://github.com/tabalinas/jsgrid/blob/master/LICENSE) */
AFAIK promises aren't supported on those callbacks, those callback are for client side processing if you want to achieve that, you should use controller method insertItem(item), as it waits for the promise, if the promise rejects it doesnt insert the item, look at my answer for issue #1320