Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are async/promises in javascript (in the browser) beneficial where there's no I/O?

I'm trying to find a good deserializer/denormalizer for json-api (which is proving surprisingly difficult).

I've come across a few examples where the deserialization process (basically just denormalizing the relationships and flattening attributes) is defined as an async function. Here's one such example, but there are many that I've found.

Now, my understanding of node/javascript is that it is predicated on systems being I/O bound, so its design is such that operations should be non-blocking so that other operations can be scheduled during I/O and thus we get concurrent operations.

What I don't understand however is the usage within a deserializer such as this. We have the full payload at the time of deserialization, there's no I/O occurring whatsoever. I can only guess that the author assumes that the relationship lookups could all happen concurrently, however, since javascript is still single threaded, I can't see how this could in any way improve performance.

It seems to me this is just making a deterministic operation non deterministic (since I suppose the schedule could also schedule other operations besides the deserialization).

Am I missing something here? Is there truly a benefit to making this asynchronous? I'm not a front-end (or a node) developer so I feel like I'm missing something (since I've seen this pattern used in deserializers a LOT)

This will be run in the browser (not a node backend) if it makes any difference.

like image 674
brad Avatar asked Mar 26 '19 13:03

brad


People also ask

Should I use async or promises?

Use promises whenever you are using asynchronous or blocking code. resolve maps to then and reject maps to catch for all practical purposes. Make sure to write both .

Are promises asynchronous in JavaScript?

A promise is used to handle the asynchronous result of an operation. JavaScript is designed to not wait for an asynchronous block of code to completely execute before other synchronous parts of the code can run. With Promises, we can defer the execution of a code block until an async request is completed.

What are the advantages of using promises instead of callbacks?

They can handle multiple asynchronous operations easily and provide better error handling than callbacks and events. In other words also, we may say that, promises are the ideal choice for handling multiple callbacks at the same time, thus avoiding the undesired callback hell situation.

When should I use async JavaScript?

Async/Await makes it easier to write promises. The keyword 'async' before a function makes the function return a promise, always. And the keyword await is used inside async functions, which makes the program wait until the Promise resolves.


1 Answers

As it seems, the authors of the library that you mentioned as an example do not use async / await correctly:

 // see: https://github.com/wopian/kitsu/blob/master/packages/kitsu-core/src/deattribute/index.js

 // v does await nothing, as it receives an array
 //                v unneccessary
 await data.map(async el => deattribute(el))

There is no reason to use async / await at all here, so I doubt that it serves any purpose in the library.

Are async/promises in javascript (in the browser) beneficial where there's no I/O?

No. Although promises are always resolved asynchronously , they will still end up in the so called microtask queue (in browsers), which will be emptied before the browser does rerender, therefore calling async functions will not help you to unfreeze the UI.

I can only guess that the author assumes that the relationship lookups could all happen concurrently, however, since javascript is still single threaded, I can't see how this could in any way improve performance.

I agree, this won't increase performance at all.

It seems to me this is just making a deterministic operation non deterministic (since I suppose the schedule could also schedule other operations besides the deserialization).

No, it could not, as there is no asynchronous IO involved that would free the engine from it's current task. Therefore it will still run in a blocking way.

like image 86
Jonas Wilms Avatar answered Oct 15 '22 19:10

Jonas Wilms