0
function getPageT()
{
    Request.get({url: "https://cardanoexplorer.com/api/blocks/pages/total"}, (error, response, body) => {
        if (error) {
            return console.dir(error);
        }
        body = JSON.parse(body)
        var total = body.Right

        return Promise.resolve(total)
    });
}

async function assign() {
    var t = await getPageT()
    console.log(t)
}

I would like the code above to print the total value but it comes as undefined because the getPage function does not wait for the request to be resolved.

How can I get the promise out of the Request.get ?

user3755529
  • 1,050
  • 1
  • 10
  • 30

1 Answers1

1

Your function need to return a promise if you want the await keyword to actually wait for something to happen:

function getPageT()
{
    return new Promise(function(resolve, reject) {
        Request.get({url: "https://cardanoexplorer.com/api/blocks/pages/total"}, (error, response, body) => {
            if (error) {
                console.dir(error)
                reject(error);
            }
            body = JSON.parse(body)
            var total = body.Right

            return resolve(total)
        });
    });
}

async function assign() {
    var t = await getPageT()
    console.log(t)
}
Morphyish
  • 3,932
  • 8
  • 26