I have a REST end point and trying to hit by using 'request' library in node.js by using the below file:
I just updated this question with latest observations.
Added new function called delayFunction() as below:
Util.js
request = require('request')
var delay1 = {
delayFunction: function() {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve(42); // After 3 seconds, resolve the promise with value 42
}, 3000);
});
}
}
var getJSONValue1 = {
getJSON: function(){
let link=[];
let file ="http://localhost:8080/idsearch/environment/amazon/cartId/2342cd1210a5";
var headers ={
"accept":"application/json"
}
var options = {
url: file,
headers: headers
}
request(options, function(error,response, body){
if(response.statusCode==200)
{
//return response.body {This is also not working}
console.log('yes');
link.push(response.body);
await delay1.delayFunction();
}
});
console.log(link);
}
}
module.exports = {
getFunction: function(){
return getJSONValue1.getJSON();
}
}
The above payload I'm trying to return the response body and printing into test.js file as below:
test.js
var util = require(__dirname + '/util');
let value=util.getFunction();
console.log(value);
But I'm unable to get the response and It says undefined as below output.
Output:
[]
undefined
yes
Updated Code:
Observed error:
await delay1.delayFunction();
^^^^^
SyntaxError: await is only valid in async function
Can anyone help me to get the response body which is only fly and store it into variable and use it for further reference.