Last active
September 2, 2019 17:42
-
-
Save lmzach09/66a45353ccd150257ad3547f706e4e3c to your computer and use it in GitHub Desktop.
Implementation of a for-each loop in JavaScript that works with callbacks. The next iteration in the loop does not start until the previous iteration's callback gets called.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // request method implementation is in a GitHub Gist linked below | |
| const array = [1, 2, 3, 4, 5]; | |
| array.forEachWithCallback((el, i, next) => { | |
| request({ | |
| method: 'GET', | |
| hostname: 'httpbin.org', | |
| path: '/get?myArg=' + el | |
| }).then((res) => { | |
| const responseBody = JSON.parse(res.body); | |
| console.log(responseBody.args.myArg); | |
| next(); | |
| }).catch((err) => { | |
| console.error(err); | |
| }); | |
| }); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/lmzach09/38d291edb90f64d338132824a9de35ce