I am currently working on creating a generator function that produces an async generator. This Generator will yield a deferred value at each time, using promises. The values and their respective delays (in milliseconds) are sourced from two separate arrays.
For the provided example code, the expected output is a sequential logging of each value
from the target
array along with its corresponding (matching index) delay
from the timeArray
. Each value should be logged one after the other, waiting for the specified delay, until the end of the iteration through the target
array.
I attempted to run the code below, however it only displays the first 2 elements in the timeArray
and does not log all the subsequent elements. I am uncertain if it prints them after the first two time intervals specified in the timeArray
.
let j = 0;
// Array representing the delay before printing each number.
var timeArray = [6, 68, 51, 41, 94, 65, 47, 85, 76, 136];
// Array containing the numbers to be printed.
var targets = [9, 10, 8, 7, 9, 7, 7, 9, 9, 7];
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();
}
}());