1

I've build a function that should validate via AJAX / POST if the entered value is valid or not. When it's not valid, I'm returning a 400 error from the function. The strange thing is now that I'm also getting an Uncaught (in promise) undefined and I don't know why. This is my code:

form.querySelector( "button[type=submit]" ).addEventListener( "click", async function ( e ) {
    let fieldA   = jQuery( "#fieldA" ),
        fieldB   = jQuery( "#fieldB" ),
        promises = [];

    promises.push( await validate( "test", fieldA ) );
    promises.push( await validate( "test", fieldB ) );

    Promise.all( promises ).then(
        console.log( "Everything is resolved successfully! You can continue with other stuff now." )
    );
} );

function validate( type, input ) {
    let data = {
        action: "validate",
        type: type,
        value: input.val()
    };
    return new Promise( function ( resolve, reject ) {
        jQuery.post( account_object.ajax_url, data, function () {
        } ).success( function () {
            resolve();
        } ).fail( function () {
            reject();
        } );
    } );
}
Mr. Jo
  • 4,946
  • 6
  • 41
  • 100

0 Answers0