How to write it without async/await? On the project, I can use only callbacks and promises.


How to write it without async/await? On the project, I can use only callbacks and promises.

'use strict'; const request = require('request'); function httpGet(opts) { return new Promise((resolve, reject) => { request(opts, (err, res, body) => { if (err) reject(err); resolve(body) }); }) } async function main() { let i = 0; while (i !== 5) { let resp = await httpGet('http://ift.tt/2ubey7y;); i = Number(JSON.parse(resp).now['epoch'].toString().slice(-1)); } return i } main().then(v => console.log("RES:", v)) .catch(err => console.error("ERR:", err)); 

Submitted July 11, 2017 at 02:55AM by sam_ua
via reddit http://ift.tt/2uK0mzc

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s