+ 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.

30th May 2019, 1:58 PM
Yuri Akim
Yuri Akim - avatar
2 Réponses
30th May 2019, 2:34 PM
Gordon
Gordon - avatar
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.
30th May 2019, 9:15 PM
Yuri Akim
Yuri Akim - avatar