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

Time:06-15

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);
            }
        });
        });
}```

CodePudding user response:

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) {

CodePudding user response:

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

  • Related