5 ms·
> The language provides 3 options for asynchronous handling and doesn’t care what path you take. The language actually just provides one: async/await.
by beeboobaa 3y ago
> The language provides 3 options for asynchronous handling and doesn’t care what path you take.
The language actually just provides one: async/await.
- austin-cheney 3y agocallbacks, promises, async/await. That is 3.
- beeboobaa 3y agoCallbacks are just functions, promises are part of a standard library (not the language), only async/await is part of the language itself.
- lucideer 3y agoPromises are part of the language. Async/await are just syntax around Promises - they're all 1 feature.
- anamexis 3y agoTechnically, await isn't specific to promises. It works with any then-able object.
- culi 3y agoWhat are other then-able objects besides Promises?
- anamexis 3y ago{ then: (callback) => callback("foo") } Anything that has the same signature as a Promise. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise#thenables https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- culi 3y agoOkay, but nothing built in to the standard. In fact, even the docs you linked only point out that other implementations of Promises are "then-able". I guess I was under the impression that there were other, non-Promise, examples of thenables
- lucideer 3y agoThe main use I've seen is returning "promise-like" objects in functions that can be sync or async depending on params but are expected to have a common return type.
- culi 3y agoTo add to this, async/await is not a complete replacement. As in, it doesn't mean you'll never have to write `Promise` in your code. You still have to rely on Promise.all when you want to run multiple promises concurrently instead of sequentially
- lucideer 3y agoThe language provides Promise objects[0]. Async/await is just syntactic sugar around Promises - every await awaits a Promise object, every async returns one. Also, it's a bit weird to say callbacks -vs- promises. Callbacks aren't inherently async in themselves; there are some native async APIs that accept callbacks - the callbacks themselves are just functions, its the native APIs that are async. Also, Promises take and execute callbacks. We're still using callbacks. What Promise objects did (for the first time) was give programmers the power to implement async functionality in the native language themselves, outside of native API calls to external functionality. [0] https://tc39.es/ecma262/multipage/control-abstraction-objects.html#sec-promise-objects https://tc39.es/ecma262/multipage/control-abstraction-object...
- austin-cheney 3y agoDifferences between promises and callbacks is not the concern here. There are three options for asynchronous handling whether or not they are different under the hood and regardless if this is all just vanity. What Promise objects did Callbacks already did that. Its just moving one concern from an API to a method. Nonetheless the capability was already there natively in the language, which is called the event loop. In all things, even outside of code, people express vanity as an implicit means of advertising some manner of their identity or preferences. To the contrary people impose vanity on others only as a means of compensating for their own low confidence. Confidence is generally exceptionally low in software.