If you're open to exploring alternative libraries for solving this issue, I recommend considering Kristopher Michael Kowal's Q library. This library allows you to utilize promises through Array.prototype.reduce, facilitating the iteration and resolution of promises when they are required.
DEMO
// Retrieving all buttons
var buttons = document.querySelectorAll('.button');
// Adding Click Event Handlers
Array.prototype.forEach.call(buttons, registerButtonClickHandlers);
// Triggering button click
Array.prototype.reduce.call(
buttons,
reduceButtonPromises ,
Q.when()
);
function registerButtonClickHandlers(button) {
button.addEventListener('click', function() {
// Display end time
console.timeEnd(button.innerHTML);
});
}
function reduceButtonPromises(promise, button) {
// Register promise callback
return promise.then(function() {
// Create deferred object
var deferred = Q.defer();
setTimeout(function() {
// Set start time
console.time(button.innerHTML);
// Simulate button click
button.click();
// Resolve the promise to trigger the next one
deferred.resolve();
}, 100);
// Return promise
return deferred.promise;
});
}