Created
December 13, 2018 15:15
-
-
Save onigra/9ebfc4d4aacdb2c5596d20f1f035a481 to your computer and use it in GitHub Desktop.
js async function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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