I'm using the request npm package, which is just a promise wrapped in an HTTP call. I've had issues with promises in the past. How do I get a promise to return a value so I can use it in react or get a promise to set a react state variable within that promise? I need a specific number from an API call to set a react state variable.
For code like so...
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(Number(JSON.stringify(body).split(",")[2].split(":")[1]);
});
and
GetHeadBlock = async () => {
request(options, function (error, response, body) {
body => Number(JSON.stringify(body).split(",")[2].split(":")[1]);
this.setState({numberofHeadBlock: body})
});
}