Skip to content

Instantly share code, notes, and snippets.

@sahin
Last active July 26, 2020 02:40
Show Gist options
  • Save sahin/7c5922ce5a6898670082ff7b4546c770 to your computer and use it in GitHub Desktop.
Save sahin/7c5922ce5a6898670082ff7b4546c770 to your computer and use it in GitHub Desktop.
// await sleep(1000)
window.sleep = function (time) {
console.log("sleeping for" + time)
return new Promise((resolve) => setTimeout(resolve, time));
}
console.log("sleep");
// await waitfor("ul.search-results__list");
window.waitfor = function(selector) {
console.log(selector);
if (!document.querySelector(selector)) {
console.log("waiting for " + selector)
setTimeout(waitfor, 800); // give everything some time to render
}
else {
console.log(selector + " not found")
}
}
console.log("waitfor");
// check how to do it below
window.asyncForEach = async function(array, callback) {
for (let index = 0; index < array.length; index++) {
await callback(array[index], index, array);
}
}
console.log("asyncForEach");
window.asyncForEachold = function (array, func) {
return new Promise(function (resolve, reject) {
var i = 0;
var next = function () {
i++;
if (i >= array.length) {
resolve();
return;
}
func(array[i], i, next);
};
func(array[i], i, next);
});
}
console.log("asyncForEachold");
@sahin
Copy link
Author

sahin commented Apr 5, 2020

var elements = document.querySelectorAll('span.actor-name');

asyncForEach(elements, async (element) => {
    await sleep(1000);
    element.style.color = "red";
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment