How to reject if error on "Promise" in editor

How to reject if error on "Promise" in editor

rheinertprheinertp Posts: 23Questions: 4Answers: 0

Hi,

I want to reject the insert of a new entry if a condition is not met. For this I have an ajax call against the database to check the condition.
I do not know how to avoid "getting stuck in <<Uncaught (in promise)>>" if I do not call "resolve"...

Here is my javascript code

    stepeditor.on("initSubmit", function(e,action) {
        return new Promise( function ( resolve, reject ) {
        $.ajax( {
                url: "licencemodule_process",
                type: 'POST',
                data: {
                    stepid:  (action=="create" ? null: stepid),
                    srcModuleid: stepeditor.field( 'tbladib_step.srcModuleid' ).val(),
                },
            success: function ( json ) {
                var jsonArr =  JSON.parse(json);

                if (json.indexOf("failed")>0) {
                   alert("You do not have enough licences!!\n" + jsonArr.hint);
                        // if user wanted to create a new step ...
                  if (stepid == null)  {
                      // in this case : close the form and exit ..
                     stepeditor.close();
                     return Promise.reject(false);                                      
                ...
               else  // if  ok --> create step       
                   resolve( jsonArr.result == "success" );

Replies

  • allanallan Posts: 62,333Questions: 1Answers: 10,228 Site admin

    I think this:

    return Promise.reject(false);

    should just be:

    return reject(false);  
    

    That's not the issue though - what you would really need is a try/catch around the submit() method call since that is what is triggering the initSubmit event. How are you currently triggering the submit action?

    Allan

  • rheinertprheinertp Posts: 23Questions: 4Answers: 0

    Hi,

    it is triggered by a click on "New" on the corresponding Datatable
    (i.e. "standard behavior")

        var steptable = $('#step').DataTable( {
            dom: "Bfrtip",
            ajax: {
                url: "step_process",
                type: 'POST'
            },
    ..        buttons: [
                { extend: "create", editor: stepeditor },
                { extend: "edit",   editor: stepeditor },
                { extend: "remove", editor: stepeditor }
            ],
            } );
    

    When click on "edit" - and the licence is not OK, I reset the values back to initial ones and call the "resolve" - this works without problems.
    It is just the "New" that poses problems - as I need to stop the insert of data into the corresponding table ..

This discussion has been closed.