I first wrap JQuery ajax like this:
function Ajax(settings) {
let p = $.ajax(settings).then(
(data, textStatus, jqXHR) => ({ data, textStatus, jqXHR }),
(jqXHR, textStatus, errorThrown) => { throw ({ jqXHR, textStatus, errorThrown }); });
return Promise.resolve(p);
}
I also have:
function to (promise) {
return promise
.then(data => [null, data])
.catch(err => [err, undefined])
}
I then do:
const [arr, res] = await to(Ajax(...));
if (err) return;
let d = res.data;
Which works well.
However I then tried like this:
async function Await(promise) {
return await promise
.then(data => [null, data])
.catch(err => [err, undefined]);
}
and
const [arr, res] = Await(Ajax(...));
if (err) return;
let d = res.data;
I can see the Ajax runs and Await
then clause gets called with the correct object however I then get:
unncaught (in promise) TypeError: Await is not a function or its return value is not iterable
What is wrong here ?