Is there a more streamlined way to determine if the subscriber has finished executing or return something and catch it up-stream? Consider the following code snippets:
this._subscriptions.push(this._client
.getCommandStream(this._command) // Returns an IObservable from a Subject stream
.subscribe(msg => {
// Processing and promise handling
http.request(url).then(
// Additional actions
);
});
To confirm when the subscription is complete, I have implemented the following approach:
this._subscriptions.push(this._client
.getCommandStream(this._command)
.subscribe(msg => {
// Processing and promise handling
http.request(url).then(re => {
// More actions
msg.done()
}).catch(err => msg.done(err));
});
By including a done
method in the object passed in, we can determine when the process is finished. However, calling done
in every promise or catch block may become tedious. Is there a more efficient and automated alternative?
The provided examples may not be optimal. In this implementation, RX is used to construct an internal messaging bus with the get command stream returning a read-only channel (as an Observable) for processing commands. This processing could involve various tasks such as an HTTP request, file input/output, or image processing.
this._client
.getCommandStream(this._command) // Returns an IObservable from a Subject stream
.subscribe(msg => {
// Processing and promise handling
http.request(url).then({
// More actions
}).then({
// Performing file io operations
if(x) {
file.read('path', (content) => {
msg.reply(content);
msg.done();
});
} else {
// Other processing tasks
msg.reply("pong");
msg.done()
}
});
});
While this usage of the Observable pattern seems appropriate for managing command sequences, frequent calls to msg.done()
throughout the code raise concerns about efficiency. What would be the best strategy to reduce these calls and accurately determine when the entire process is completed? Alternatively, should everything be wrapped in a Promise, and how does this differ from using resolve
instead of msg.done()
?