V
V
vetsmen2019-10-11 18:14:33
JavaScript
vetsmen, 2019-10-11 18:14:33

Is it okay to communicate with microservices through promises?

Is it normal practice to communicate with microservices through promises?
There is a similar design:

const getData = (data) => {
    return new Promise((resolve, reject) => {
        rpc.send(data);
        rpc
            .onRequest(newData => resolve(newData))
            .onError(error => reject(error));
    })
}

where RPC is some kind of protocol through which communication with the microservice takes place.
In this regard, questions arise:
1) What if the response from the microservice is long, and there will be many requests generated? Indeed, according to the idea, the call stack can overflow due to a bunch of promises with a pending state.
2) What if the answer does not come at all? After all, the promise is not simply rejected, again we run into a stack overflow. The first thing that comes to mind is setTimeout , which will itself reject the promise after a certain time, but then the data will be lost, which could possibly come later.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Sokolov, 2019-10-11
@vetsmen

Practice is normal.
Both "what if" problems are contrived and irrelevant.
What specifically would you like to do in case of a delayed response? Let's say we set a threshold of 10 seconds. Still no answer.
The options, in fact: wait some more, or reject. You can also imagine the option - to abort the previous attempt and send the same thing again.

V
Vitaly, 2019-10-12
@vshvydky

Look at nest there is already an implementation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question