Skip to content

Instantly share code, notes, and snippets.

@lmzach09
Created September 2, 2019 18:41
Show Gist options
  • Save lmzach09/55307ef421cec0cce6b424ac625eae84 to your computer and use it in GitHub Desktop.
Save lmzach09/55307ef421cec0cce6b424ac625eae84 to your computer and use it in GitHub Desktop.
An example of a while loop implementation using the async/await pattern. This works in the browser and Node.js. Get the "request" method here https://gist.github.com/lmzach09/38d291edb90f64d338132824a9de35ce#file-request-js
const request = require('./request.js');
const array = [1, 2, 3, 4, 5];
(async function() {
while (array.length > 0) {
// Dequeue the first element in the array
// Note that this modifies the array!
const el = array.shift();
const response = await request({
method: 'GET',
hostname: 'httpbin.org',
path: '/get?myArg=' + el
});
const responseBody = JSON.parse(response.body);
console.log(responseBody.args.myArg);
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment