jsgrid icon indicating copy to clipboard operation
jsgrid copied to clipboard

Promises in onItemInserting not working

Open grahams777 opened this issue 5 years ago • 1 comments

  1. 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:

  1. The methodology to obtain server data works correctly outside the jsTable framework and employs ES6 async/await.

  2. 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) */

grahams777 avatar May 10 '20 13:05 grahams777

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

Ex0-Ghost avatar Oct 09 '20 14:10 Ex0-Ghost