Skip to content

Instantly share code, notes, and snippets.

@popovkos
Last active May 24, 2018 21:13
Show Gist options
  • Save popovkos/132548d52758c68cf916bd76f29a3cb9 to your computer and use it in GitHub Desktop.
Save popovkos/132548d52758c68cf916bd76f29a3cb9 to your computer and use it in GitHub Desktop.
async function someFunction() {
const myArray = [1, 2, 3];
const connection = mysql.createConnection({ options }); // create db connection
let finalArray = [];
await asyncForEach(async (value) => {
const finalValue;
const asyncFunctionValue = await asyncFunction(connection, value); // async function that returns a promise
finalValue.asyncFunctionValue = asyncFunctionValue; // doing assignment operations with resolved value
finalArray.push(finalValue); // pushing each value to an array
});
return functionThatUsesResolvedValues(finalArray); // passing array of values futher
};
async function asyncForEach(array, callback) { // modifies version of forEach, taken from https://codeburst.io/javascript-async-await-with-foreach-b6ba62bbf404
for (let index = 0; index < array.length; index += 1) {
await callback(array[index], index, array);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment