jQuery 3 Deferred objects are supposed to be Promises/A+ compatible. From this question, it seems that the TypeScript compiler will yell at you for it. But this code compiles fine using the latest version of jQuery and TypeScript:
import $ from 'jquery';
(async () => {
const response = await $.getJSON('https://api.github.com/');
console.log(response);
})();
If the value of the expression following the
await
operator is not aPromise
, it's converted to a resolved Promise.
I don't see an issue here considering that the expression should already be returning a Promise.
- Does this mean it is safe to use
await
with$.ajax
(and friends such asgetJSON
,post
, etc.)? Or are there cases where the code will fail? - Why is it OK to wrap
Promise.resolve()
around a jQuery 2 Deferred object that is not Promises/A+ compliant to make it a realPromise
, butawait
ing on a jQuery 2 Deferred object will fail?
I've referred to a TypeScript example above but I'm asking this question in the general context of JavaScript. If there are differences in how TypeScript compiler handles await
versus how Node.js/Babel/browsers handles it in JavaScript, please elaborate in your answer.