preSubmit not cancelling when returning false from an async/await handler

preSubmit not cancelling when returning false from an async/await handler

geometrygeometry Posts: 26Questions: 8Answers: 0

It seems that an async/await handler is not an expected argument for event handlers; e.g.

editor.on("preSubmit", async () => {
    try {
        return (await jQuery.ajax(...)) as boolean;
    } catch (e: any) {
        alert(e);

        return false;
    }
});

Is this type of programming structure acceptable when using event handlers?

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 65,347Questions: 1Answers: 10,841 Site admin

    I would expect that to work okay. I've just tried making a little example and it works okay.

    Can you link to a test case showing the issue please? Also, what version of Editor are you using?

    Allan

  • geometrygeometry Posts: 26Questions: 8Answers: 0

    Hi Allan,

    Our company is using DataTables Editor v2.4.0.

    I have also looked at your example and it doesn't replicate the scenario I'm concerned with. Perhaps I wasn't clear in the description.

    In the scenario that I having problems with is that the async/await handler is returning false to cancel the submit process. It doesn't cancel the submit process, but instead cycles to run the async/handler again. It gets into an endless loop.

  • allanallan Posts: 65,347Questions: 1Answers: 10,841 Site admin
    Answer ✓

    A promise that resolves to false should still work okay - here is my example updated to show that.

    I've just had a look at the 2.4.0 code and there hasn't been any changes in that section between it an 2.5.0, so it should work there.

    Are you able to modify my example to demonstrate the problem or give me a link to a test case showing the issue?

    Thanks,
    Allan

  • geometrygeometry Posts: 26Questions: 8Answers: 0

    Hi Allan,

    I think I found the source of my problem.

    I was using the async/await pattern extensively and returning a boolean which would have been converted to a Promise<boolean>. But in this instance it would have been unresolved. This could be wrong. But having resolved promises and avoiding the async/wait pattern seemed to fix the problem.

    Anyway, I have moved past this problem.

    Thanks for your support.

Sign In or Register to comment.