V
V
Vitaly2016-12-11 10:12:13
Node.js
Vitaly, 2016-12-11 10:12:13

How to make asynchronous request in getter?

Greetings. The task is as follows: in the class property getter, load data over the network. What for? This is a matter of convenience and is still experimental in nature with the goal of diving into the intricacies of node.js. Now I'm not interested in necessarily the correct proven methods of solving - any working crutches will do. But I will immediately mention a few already known.
1. A synchronous request is not an option because the poor event loop will sleep before the response is returned.
2. async / await - it would be great if this couple showed itself here, but in this case the getter will return a promise when it should return a value.
3. Multithreading with the suspension of the thread from the main process - so far the most attractive option, if you do not take into account the load of an unlimited number of threads. Although it is, in principle, decided by the pool, but this creates additional barriers to the number of simultaneous requests.
Ideally, I would like to somehow put a stub in this function, as yield does, in order to catch the event loop upon return and continue from the same place in one thread. But it is this operator in the getter that will not bring any benefit.
Dear experts, I'm waiting for your suggestions)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2016-12-11
@vitali1995

Getters are synchronous in nature, the only option is to return a promise
Well, in general, getters are not designed for this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question