Skip to content

Provides an asyncForEach function to await promises inside the loop

Notifications You must be signed in to change notification settings

rodrigoyoshida/async-await-foreach

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Async Await forEach

This is a simple module that provides an asyncForEach function that allows promises to be awaited inside the loop. The asyncForEach function is also asynchronous so it needs to be treated as a promise (use then or await).

Install

You can install this module using your favorite module manager (Yarn or NPM)

yarn add async-await-foreach
npm install async-await-foreach

Import

You can import it as a RequireJS module and ES6 module, as following:

const asyncForEach = require('async-await-foreach') 
import asyncForEach from 'async-await-foreach'

Usage

Using 'then' to execute code after finishing the forEach loop

let myArray = [1, 2, 3]

asyncForEach(myArray, async item => {
  await myPromise(item)
}).then(() => {
  console.log('done')
})

Using inside another async function

let myArray = [1, 2, 3]

await asyncForEach(myArray, async item => {
  await myPromise(item)
})

console.log('done')

About

Provides an asyncForEach function to await promises inside the loop

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published