what I am trying to do might not be possible in node js, but before I give up I want to get other opinions.
I have a thread which is performing the setup of a product. For the sake of argument, let us call it the 'main' thread. I want all errors that may occur during the product set up to be caught by the 'main' thread. If the setup is successful, then I do not want the 'main' thread to finish before I know that the setup has been successful.
One of the steps during the product setup involves creating a CloudFormation stack in the customer's AWS account. The AWS function to do this is createStack. This function merely starts off the process of creating the stack. If you call this function using await, then the process of creating the stack will still be running after you get the return value.
There is another AWS CloudFormation function called describeStacks which I can use to find out how the creation is going. I cannot call this method repeatedly (without using a large interval between calls) as AWS has limits on how often I can call methods.
I would like to be able to call this function once every minute to check on the progress of the stack creation, until I can determine that the setup has either succeeded or failed. But crucially, I also want the 'main' thread to wait until either this success or failure has occurred.
Is this possible in node js? I am new to node js. Do feel free to tell me that I am misusing it and that I am thinking about the problem in the wrong way. I suspect I am.
SOLUTION: Here is the code that solved the problem for me. Thanks to Tomalak
function checkStackCreationProgress(params) {
return new Promise( (resolve, reject) => {
const intervalId = setInterval( async () => {
try {
const result = await checkProgress(params);
switch(result.status) {
case 'IN_PROGRESS': break; //do nothing
case 'COMPLETE': clearInterval(intervalId); resolve(result); break;
case 'FAILED': clearInterval(intervalId); reject(result); break;
}
}
catch(error) {
clearInterval(intervalId);
reject(error);
}
},
60000);
})
.then( () => {
return;
})
.catch(error => {
throw new SetupFailedError(`Failed to create ...`, error);
});
}
This function will not return until the setup has successfully completed or failed