Alternatives to request-promise-native
Just for having another option in mind, I would suggest using the node native http
module.
import * as http from 'http';
async function requestPromise(path: string) {
return new Promise((resolve, reject) => {
http.get(path, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
resolve(data);
});
}).on("error", (error) => {
reject(error);
});
});
}
(async function () {
try {
const result = await requestPromise('http://www.google.com');
console.log(result);
} catch (error) {
console.error(error);
}
})();
I'd strongly suggest using node-fetch. It is based on the fetch API in modern browsers. Not only is it promise-based it also has an actual standard behind it.
The only reason you wouldn't use fetch
is if you don't like the API. Then I'd suggest using something cross-platform like axios or superagent.
I personally find using the same API on the server and browser eases maintainability and offers potential for code reuse.
On the same github issue for request
there is another link which talks about the alternatives. You can see them here. It clearly explains different types and wht style they are (promise/callback).