4 ms·
I'm not sure about the no promises and no callbacks part. The similar line they use in their examples doesn't define a separate getJsonAsync function in the "A
by xlm1717 11y ago
I'm not sure about the no promises and no callbacks part.
The similar line they use in their examples doesn't define a separate getJsonAsync function in the "Asynchronous city" section (and, as far as I can tell, there's no native getJSON function). That, and the fact that the line immediately preceding the await call has a comment that says, "just have to await the promise!", leads me to have to assume they used the function defined in the promises section, which of course uses promises. And, in order to define that promise, they do have to use three callbacks.
With functions and just callbacks I can make one-liners for fetching data too if I abstract away all the stuff that actually does the fetching of the data:
getJSON('/users/joe/orders', function(orders) {} );
- nvivo 11y agoThe parent exaggerated a little. async/await does use promises. In fact, it's built around them. All async functions return promises, and all await expressions await on promises. Currently on babel implementation with facebooks's regenarator it seems to work on any "thenable", because I got it working with jquery and angular promises.
- callahad 11y agoDepending on which transforms you enable, you can also eliminate Regenerator and use native ES6 promises and generators. E.g: https://gist.github.com/callahad/b99c83d6d9fd675137b7 https://gist.github.com/callahad/b99c83d6d9fd675137b7 Promises are sufficiently supported in the latest versions of all major browsers, and Generators are almost there. They're supported in Firefox and Chrome, behind a flag in Edge, but unsupported in Safari. But if you're targeting Node 4.0, you might as well transpile down to ES6 instead of going all the way to ES5 and pulling in Regenerator :)
- callahad 11y ago> as far as I can tell, there's no native getJSON function There is a standard, native fetch() function which is pretty darn similar: https://hacks.mozilla.org/2015/03/this-api-is-so-fetching/ https://hacks.mozilla.org/2015/03/this-api-is-so-fetching/