Skip to content

Instantly share code, notes, and snippets.

@onigra
Created December 13, 2018 15:15
Show Gist options
  • Save onigra/9ebfc4d4aacdb2c5596d20f1f035a481 to your computer and use it in GitHub Desktop.
Save onigra/9ebfc4d4aacdb2c5596d20f1f035a481 to your computer and use it in GitHub Desktop.
js async function
const http = require("http");
const util = require("util");
function post(payload) {
return new Promise((resolve, reject) => {
const options = {
method: "POST",
hostname: "httpbin.org",
port: 80,
path: "/post"
};
const req = http.request(options, res => {
res.setEncoding("utf8");
let body;
res.on("data", chunk => {
body = chunk;
});
res.on("end", () => {
resolve(body);
});
});
req.on("error", err => {
reject(err);
});
req.write(util.format("%j", payload));
req.end();
});
}
// post("foobar")
// .then(res => {
// console.log(res);
// })
// .catch(err => {
// console.log(err);
// });
async function asyncFunc() {
try {
const response = await post("foobar");
console.log(response);
} catch (err) {
throw err;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment