0

I have an async function that needs to call itself after a set timeout if a certain criteria is not met.

Calling the async function via setTimeout fails saying it is expecting a function and got a promise.

I have searched on here and I am still at a loss how I should proceed. I have tried hiding it in a synchronous function w/o success. I have not tried then/catch yet.

Code:

//Contact the database and external api...

async function getAlertData(uris, id, page){
    let timer = 60;
    let key = uris.key;
    delete uris.key;
    let dataArray = [];
    let elapsed = (moment()-page.sent)/1000/60;

    for await (const uri of uris) {
        let config = {
            method: 'get',
            url: uri.uri,
            headers: {
                'Authorization': 'Bearer '+key
            }
        }
        let alert = await axios(config);
        alert.data.message.alert.uri = uri.uri;
        dataArray.push(alert.data.message.alert)      
    }
    let alertData = dataArray.filter(alert=>{
        return id.includes(alert.cad_code) && alert.cad_code !== ''
    })
    let allResponses = alertData[0].responses;
    let yesResponses = allResponses.filter(each=>{
        return each.response === 'Yes'
    })


     //Here is the logic to call the function again if needed.

    let enoughCareer = {enough: false};
    if(yesResponses.length > page.numNeeded){
        enoughCareer = await checkCareer(yesResponses, page.numNeeded)
    }

    console.log(enoughCareer);

    if(enoughCareer.enough === false && elapsed < timer){
        console.log('Not enough PS. Less than '+timer+' minutes. Checking again.');

////////////////////////////////////////////////////////////////////////////////////////
// This setTimeout fails saying it is expecting a function and got a promise (async function). 

        setTimeout(restartAlertData(uris, id, page), 5000);
//////////////////////////////////////////////////////////////////////////////////////////

    }else{
        console.log('Award Partial Recall/Fail');
    }    
}
  • `restartAlertData` must return a Promise, and `setTimeout` expects a function as its first argument. – Bucket May 27 '20 at 18:25
  • Thanks guys! The link above helped a ton. I attempted to do something very similar by hiding the async function inside another function but I did not try the anonymous function within setTImeout. Now I can call it without any issue. THANKS! – A Wilkinson May 27 '20 at 21:22

0 Answers0