Convert Promise Resolve To Readable Data In The Called Function
im doing some online test , which i need to call a function , inside that function send an http request to an api and return its response function callapi() { require('https')
Solution 1:
I would suggest you try to use async
and await
.
eg: function async callapi()
and return await new Promise(function (resolve, reject)
Solution 2:
If you don't want to use the promise, then you can make a sperate function(which performs the operation on returned data) and passed it to callapi function as a parameter.
functioncallapi(callback)
{
require('https').get(`https://example.com/api/getdata`, (res) =>
{
res.setEncoding('utf8');
res.on('data', function (body)
{
var data = JSON.parse(body);
//return(data);callback(data);
});
});
}
Post a Comment for "Convert Promise Resolve To Readable Data In The Called Function"