I expected the output: A, B, C. But it doesn't work. Provided that the function handleClick(element) cannot be changed, how can I change the other functions to make sure that all code execute in sequence and output A, B, C as expected?
async function handleClick(element) {
setTimeout(function(){
console.log(`Click on Element_${element}`);
}
, Math.random(5)*1000);
}
async function clickLetter(letter) {
await handleClick(letter);
}
async function clickGroup(group) {
await handleClick(group);
}
const letters = ['A', 'B', 'C'];
function clickLetters(letters, fn) {
let index = 0;
return new Promise(function(resolve, reject) {
function next() {
if (index < letters.length) {
fn(letters[index++]).then(next, reject);
} else {
resolve();
}
}
next();
});
}
clickLetters(letters, clickLetter);
asynclibrary, caolan.github.io/async/docs.html#eachSeriesasync.eachSeries(letters, clickLetter)?clickLetterreturns a promise, it does not take a node-style callback.