What is a way to force my code to wait inside a promise?

so I currenlty know what the issue is, it’s the fact that my await here var contacBID = await ContactIDFinder.findingBoondIDcontact(ContactHID) can not be used here. But he problem is: I need to wait in order to avoid just getting a promise, and this has to include a Promise since it will also be used later with a promise. Any ideas? Feel free to call my code ugly, I know it is, but any help is appreciated, thanks!

    var optionsA = {
        method: "GET",
        url: "https://example.com",
        qs: { hapikey: "smthng" }
    };
    return new Promise(function(resolve, reject){
        request(optionsA, function (error, response, body) {

        if(error){
            logResponse("could not reach boondmanager status:",response.statusCode);
            reject("error while reading");
            }

        else{
            var dealH = JSON.parse(body);
            if(dealH.associations.associatedVids[0] != undefined){
                var ContactHID = dealH.associations.associatedVids[0]
                var contacBID = await ContactIDFinder.findingBoondIDcontact(ContactHID)
                console.log(`attempt at finding the BID for the contact linked to the company led to : ${contacBID}`)
                if (contacBID != null) { 
                    console.log("associated BID is : "+ contacBID)
                }
                else{
                    console.log("linked with a contact unsynchronized")
                }
            }
            else{
                console.log("Not linked at all with a contact")
                resolve(null)
            }
            resolve(contacBID);
            }
        });
        });
}```

>Solution :

You can use await there if you mark the enclosing function as async.

Change:

request(optionsA, function (error, response, body) {

to:

request(optionsA, async function (error, response, body) {

Leave a Reply