I am trying to determine a way to "pause" my Promises code until a condition is true, perhaps by using a recursive setTimeout().  For my simple example, I am manually setting waitValue.
I need to wait for a moment, and if waitValue is still false, then merely continue to wait.  And of course when waitValue becomes true, then continue processing.   Here is what i have pieced together so far:
var counter=0;
function promiseTest() { 
    return new Promise(  (resolve, reject) => {
        if  ( waitValue )  {
            console.log('waitValue is now true');
            resolve('FIRST PROMISE');
        } else  {                      // we need to wait again
            if  ( counter > 1000 ) {   // dont wait forever.
                reject('waited too long');
            } else {
                console.log('WAIT MESSAGE: ' + counter++ );
                setTimeout( promiseTest, 3000);
            }
        }
    })
    .then(   result => {
        return(`SECOND PROMISE:  the result is: ${result}`);
    });
}
And to use it:
promiseTest().then( (result)=>{ console.log('LOCAL MESSAGE: ' + result); });
The following works fine:
var waitValue = true;
promiseTest().then( (result)=>{ console.log('LOCAL MESSAGE: ' + result); });
// waitValue is now true
// LOCAL MESSAGE: SECOND PROMISE:  the result is: FIRST PROMISE
However, the following does not seem to complete as i wanted:
var waitValue = false;
promiseTest().then( (result)=>{ console.log('LOCAL MESSAGE: ' + result); });
// waiting messages appear as expected
waitValue = true;
// waitValue is now true
// no other messages
I have been unable to find a promises example to temporarily execution. An ordinary javaScript example might look like this:
var waitValue = false;
var counter = 0;
(function tester() {
   if  ( waitValue ) {
      console.log('finally true');
   } else {
       if  ( counter > 1000 ) {
           console.log('waited too long');
           process.exit;
       } else {
           console.log('still waiting, counter = ' + counter++);
           setTimeout(tester, 1000);
       }
   }
})();
// wait a few seconds and enter this into the console:
var waitValue = false;
What would a promises script look like to temporarily pause execution? Or maybe Promises should not be used like this at all?
Thank you very much.
 
     
     
    