Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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) {
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading