Skip to content

Instantly share code, notes, and snippets.

@SMoni
Created May 7, 2022 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SMoni/402c630895e1c35fdaa535ad3a5a7fd8 to your computer and use it in GitHub Desktop.
Save SMoni/402c630895e1c35fdaa535ad3a5a7fd8 to your computer and use it in GitHub Desktop.
const myArray = Array.from({length: 10}, (e, i)=> i)
doWork(
doSomethingWith,
myArray,
logToConsole
)
//Die Funktion, die das Ganze abarbeitet
async function doWork(doSomething, anArray, aCallback) {
//Mit for(const ... in ...) { ... } geht es
for(const entry in anArray) {
await doSomething(entry)
aCallback(entry)
}
}
//Hier passiert etwas, was länger dauert
function doSomethingWith(entry) {
console.log(`handling ${entry}`)
return new Promise(resolve => setTimeout(resolve, 1000))
}
//Das soll ein callback sein
function logToConsole(value){
console.log(`done with ${value}`)
console.log('--------------------')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment