Skip to content

Instantly share code, notes, and snippets.

@cnayan
Created December 7, 2017 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cnayan/f0c47f6718856b95f726f42cb0a06f51 to your computer and use it in GitHub Desktop.
Save cnayan/f0c47f6718856b95f726f42cb0a06f51 to your computer and use it in GitHub Desktop.
JavaScript "request" callbacks transformed to async/await
const requestAsync = require('./requestAsync');
let options = {
uri: 'http://localhost:3000/endpoint',
method: 'POST',
json: {} // your data
};
async function main() {
let result = await requestAsync(options);
// rest of the code using result
}
main();
const request = require('request');
module.exports = function requestAsync(options) {
return new Promise((resolve, reject) => {
try {
request(options, (err, res, body) => {
if (err) {
reject(err);
} else {
resolve({ res, body });
}
});
} catch (error) {
reject(error);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment