Reputation: 775
I am using request-promise-native to make a http request in my Node.js application. I want to convert this to an RxJS observable but i don't know how to unsubscribe. What is an efficient methods to do this?
public getResponse(req: Request, res: Response) {
const url = config.url;
var options = {
uri: `${url}`,
body: JSON.stringify(providerResponse),
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
const responseObs = from(request(options)); // convert promise to observable using from
responseObs.subscribe(response => {
res.setHeader('content-type', 'application/json');
res.send(response)
}, error => {
console.error(error)
})
};
Upvotes: 0
Views: 812
Reputation: 222369
from
converts promises to observables that emit a single value and complete when a promise is settled. The code is already usable, there's no need to unsubscribe completed observable.
Upvotes: 1