发布功能失败时未捕获(承诺)未定义

时间:2019-09-15 07:20:32

标签: javascript jquery promise

我已经构建了一个函数,该函数应该通过AJAX / POST验证输入的值是否有效。无效时,我从函数返回400错误。现在奇怪的是,我也得到了Uncaught (in promise) undefined,但我不知道为什么。这是我的代码:

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();
        } );
    } );
}

0 个答案:

没有答案