Async Await problem not working in order | Sololearn: Learn to code for FREE!

+2

Async Await problem not working in order

In the following code am trying to get request() to be executed before the return module.exports = { func: async function (servURI, sName, APIKey) { var res; const options = await { url: servURI + sName, headers: { "Accept-Charset": "application/x-www-form-urlencoded; charset=UTF-8", "Token": APIKey, "Accept-Language": "en-US,en;q=0.9,ar;q=0.8", "User-Agent": "Bm" } } async function callback0(error, response, body) { if (!error && response.statusCode == 200) { const info = await JSON.parse(body); //need res to have info.id value before continue res = info.id console.log("cb func: "+res) } else if (response.statusCode == 404) { return 0; } } await request(options, callback0); console.log("sid: "+ res); return res; } } Log: 2019-05-30T13:40:49.116699+00:00 app[worker.1]: sid:undefined 2019-05-30T13:40:49.827820+00:00 app[worker.1]: cb func: YZEBzE2BWR74QdsvS8WupVdWujUI17oPYygOf4ppO-e3AN8 I'm still new to node.js, so any help would be appreciated, Thanks in advance.

5/30/2019 1:58:19 PM

Yuri Akim

2 Answers

New Answer

+1

Use ES6 syntax Promise() and .then() https://medium.freecodecamp.org/how-to-master-async-await-with-this-real-world-example-19107e7558ad

0

@Gordon, that's for async/await not for promise, i still don't understand how to apply that to my code up there, its completely different code.