Async Await an AJAX Request

This will only work on more modern browsers! :)


async function doAjax(args) {
    let result;

    try {
        result = await $.ajax({
            url: ajaxurl,
            type: 'POST',
            data: args
        });

        return result;
    } catch (error) {
        console.error(error);
    }
}

// two ways to call this async function: 

// Elsewhere in code, inside an async function
const stuff = await doAjax();



doAjax().then( (data) => doStuff(data) )