javascript - Writing promise based HTTP request using Async/Await -


is possible rewrite following http request example node manual use async/await rather promises? familiar return new promise method resolve/reject rather use async/await.

    const postdata = querystring.stringify({       'msg': 'hello world!'     });      const options = {       hostname: 'www.google.com',       port: 80,       path: '/upload',       method: 'post',       headers: {         'content-type': 'application/x-www-form-urlencoded',         'content-length': buffer.bytelength(postdata)       }     };      const req = http.request(options, (res) => {       console.log(`status: ${res.statuscode}`);       console.log(`headers: ${json.stringify(res.headers)}`);       res.setencoding('utf8');       res.on('data', (chunk) => {         console.log(`body: ${chunk}`);       });       res.on('end', () => {         console.log('no more data in response.');       });     });      req.on('error', (e) => {       console.error(`problem request: ${e.message}`);     });      // write data request body     req.write(postdata);     req.end(); 

the code posted doesn't use promises (req event emitter, more clientrequest instance), no cannot use async/await syntax here.

i familiar return new promise method resolve/reject rather use async/await

no, cannot use async/await instead of promise constructor anyway. can use syntactic sugar then calls only.


Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -