0

I have had this working before I swear. Am currently porting a project that is very heavy on synchronous flow (async: false is heavily used, but I'm getting annoyed at the deprecation warnings.

Basically I need a function that can get me an Ajax response from anywhere in the code. What I've currently got is

let processAjaxRequest = (endpoint, data, method = 'POST') => {
    return $.ajax({
        url: endpoint,
        method: method,
        data: data,
    });
}

let makeAjaxRequest = async (endpoint, data, method='POST') => {
    return await processAjaxRequest(endpoint, data, method)
}

$(document).ready(() => {
    let result = makeAjaxRequest('/ajax/lang', '', 'GET')
    console.log(result)
})

But the document.ready() function is still outputting a promise as opposed the pure result I'm trying to await for.

Josh Dredge
  • 403
  • 1
  • 8
  • You have nothing that returns the result of the AJAX request. – Barmar May 20 '21 at 04:24
  • Doesn't jquery handle that automatically? I get the same issue when I provide `success: (result) => {return result}` as a param. The promise returns the correct data, but I was under the assumption that the makeAjaxRequest function would await the promise from processAjaxRequest. – Josh Dredge May 20 '21 at 04:31
  • No, it doesn't. You have to provide a `success:` function or use the `.done()` method to process the response. – Barmar May 20 '21 at 04:32
  • `result.done(response => console.log(response))` – Barmar May 20 '21 at 04:33
  • As mentioned, I get the same thing when I use a `success:` function. – Josh Dredge May 20 '21 at 04:40

0 Answers0