0

When I make an AJAX call with this function the value of return2 is an unfulfilled promise which is printed instantly, the then() function is not waiting for it to be fulfilled before continuing.

$.ajax({
    url: `/security/csrf`,
    type: 'POST',
}).then((data) => {
    console.log(data)
    return new Promise((resolve, reject) => {
        setTimeout(() => {resolve("DONE")}, 10000)
    })
}).then((return2) => {
    console.log(return2)
})

However when I create a promise and resolve it then the value of result2 is "DONE" as expected after 10 seconds delay

new Promise((resolve, reject) => {
    resolve("HERE");
}).then((data) => {
    console.log(data);
    return new Promise((resolve, reject) => {
        setTimeout(() => {resolve("DONE")}, 10000)
    })
}).then((result2) => {
    console.log(result2)
})

I know that the AJAX call is resolving successfully as when I run the below it logs the data out as normal and doesn't log an error

$.ajax({
    url: `/security/csrf`,
    type: 'POST',
}).then((data) => {
    console.log(data);
}, (err) => {
    console.error(err);
})
Tristan Warren
  • 435
  • 1
  • 7
  • 17

0 Answers0