Home > Blockchain >  function generator setTimeout with array as delay
function generator setTimeout with array as delay

Time:12-03

I'm trying to create a function generator prints one element in the array targets at the specified time in milliseconds in the timeArray and after the value is printed out the list will go to the next element in the time array and targets array and after the next time interval in the timeArray we print out the next value in the target array and continute to follow the same pattern until we reach the end of the array

I tried using the code shown below but it only prints out the first 2 elements in the timeArray but it doesn't print all of the other elements but I'm not sure if it's printing them after the first two time intervals in timeArray

let j = 0;
var timeArray = [6, 68, 51, 41, 94, 65, 47, 85, 76, 136];//Array that shows how long after each number printed to the console the next value should be printed
var targets = [9, 10, 8, 7, 9, 7, 7, 9, 9, 7];//Array of numbers that should be printed
let generator = generateSequence();
async function* generateSequence(casiInfluence){
       yield new Promise((resolve, reject) => {
       setTimeout(() => resolve(console.log(targetArray[j]), timeArray[j]);
                            console.log(timeArray[j]);
                        });
                    
                }
                (async function main(){
                    for await(var result of generateSequence()){
                        console.log(result);
                        j  ;
                result = generator.next();
                    }
                }());

CodePudding user response:

For each step towards a generator for deferred values the OP could implement an own task ... like ...

  • mapping both of the OP's arrays into a better processable data-structure ... here an array of tuples where each tuple is made of ... [<value>, <delay>].

  • mapping of each value-delay tuple into a deferred value action which is an async function that creates and returns a promise which will resolve the value after the milliseconds delay where the mapping function that creates the async function is called createDeferredValueAction.

  • creating the async generator from the array of async functions where the generator function is called createDeferredValuesPool.

// - a specific helper which creates an async function
//   for each to be deferred value.
function createDeferredValueAction(value, delay) {
  return async function () {
    return await (
      new Promise(resolve => setTimeout(resolve, delay, value))
    );
  };
}

// - another helper which creates an async generator
//   from an array of async functions.
async function* createDeferredValuesPool(asyncFunctions) {
  asyncFunctions = [...asyncFunctions];

  let asyncFct;
  while (asyncFct = asyncFunctions.shift()) {

    yield (await asyncFct());
  }
}


// - array that defines how long after each to be
//   processed value the next value will be processed.
const valueDelays = [600, 680, 510, 410, 940, 650, 470, 850, 760, 1360];

// - array of to be processed target values.
const targetValues = [9, 10, 8, 7, 9, 7, 7, 9, 9, 7]


// - maps both above OP's arrays into a
//   better processable data-structure.
const targetEntries = targetValues
  .map((value, idx) => [value, valueDelays[idx]]);

console.log({ targetEntries });

// - helper task which creates a list of async functions.
const deferredValueActions = targetEntries
  .map(([value, delay]) =>
    createDeferredValueAction(value, delay)
  );

// create an async generator ...
const deferredValuesPool =
  createDeferredValuesPool(deferredValueActions);

(async () => {
  // ... and iterate over it.
  for await (const value of deferredValuesPool) {
    console.log({ value });
  }
})();

console.log('... running ...');
.as-console-wrapper { min-height: 100%!important; top: 0; }

CodePudding user response:

You created the generator with let generator = generateSequence(), but then you never iterated over it.

(This is spread out and uses more variable names, so it's easier to read):

var timeArray = [6, 68, 51, 41, 94, 65, 47, 85, 76, 136];//Array that shows how long after each number printed to the console the next value should be printed
var targets = [9, 10, 8, 7, 9, 7, 7, 9, 9, 7];//Array of numbers that should be printed

var generator = generateSequence();

( async () => {

    for await ( const printNumber of generator ) {
        /* Accessing the generator's properties logs to the console, so... 
           Nothing to do in this for{} loop. */
    }

} )()

async function* generateSequence() {
    for ( const i in timeArray ) {
        const delay = timeArray[ i ];
        const numberToPrint = targets[ i ];
        await waitForPrint( numberToPrint, delay );
        yield;
    }
}

function waitForPrint( text, delay ) {
    return new Promise( (resolve,reject) => {
        setTimeout(
            () => {
                console.log( text );
                resolve();
            },
            delay
        )
    })
}

This would be easier without generators:


var timeArray = [6, 68, 51, 41, 94, 65, 47, 85, 76, 136];//Array that shows how long after each number printed to the console the next value should be printed
var targets = [9, 10, 8, 7, 9, 7, 7, 9, 9, 7];//Array of numbers that should be printed


for( let scheduledTimeMS=0, i=0; i<timeArray.length; i   ) {

    const numberToPrint = targets[ i ];
    const delay = timeArray[ i ];

    scheduledTimeMS  = delay;

    setTimeout( () => console.log( numberToPrint ), scheduledTimeMS );

}
  • Related