https://github.com/shipshapecode/ember-async-await-for-each
async/await aware forEach for Ember
https://github.com/shipshapecode/ember-async-await-for-each
async await ember-addon foreach
Last synced: 11 months ago
JSON representation
async/await aware forEach for Ember
- Host: GitHub
- URL: https://github.com/shipshapecode/ember-async-await-for-each
- Owner: shipshapecode
- License: mit
- Created: 2018-10-18T17:14:20.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-08-15T21:21:09.000Z (over 6 years ago)
- Last Synced: 2025-04-11T18:57:03.036Z (11 months ago)
- Topics: async, await, ember-addon, foreach
- Language: JavaScript
- Size: 313 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
ember-async-await-for-each
==============================================================================

**[ember-async-await-for-each is built and maintained by Ship Shape. Contact us for Ember.js consulting, development, and training for your project](https://shipshape.io/ember-consulting/)**.
[](http://badge.fury.io/js/ember-async-await-for-each)


[](http://emberobserver.com/addons/ember-async-await-for-each)
[](https://travis-ci.org/shipshapecode/ember-async-await-for-each)
[](https://codeclimate.com/github/shipshapecode/ember-async-await-for-each)
[](https://codeclimate.com/github/shipshapecode/ember-async-await-for-each/coverage)
`async/await` aware `forEach` for Ember. Concept taken from [this great article](https://codeburst.io/javascript-async-await-with-foreach-b6ba62bbf404)
on `async/await` in `forEach`.
Compatibility
------------------------------------------------------------------------------
* Ember.js v3.4 or above
* Ember CLI v2.13 or above
* Node.js v8 or above
Installation
------------------------------------------------------------------------------
```
ember install ember-async-await-for-each
```
Usage
------------------------------------------------------------------------------
First you will want to import `asyncForEach`
```js
import asyncForEach from 'ember-async-await-for-each';
```
You can then use it inside of any `async` functions and await its result.
An example of how you could use it to save addresses for a person model is below.
```js
const saveAddresses = async () => {
await asyncForEach(person.get('addresses').toArray(), async (address) => {
const address = await person.get('address');
if (address.get('isDirty')) {
return await address.save();
}
});
};
saveAddresses();
```
You can continue to wrap this in other `async` functions as well.
```js
const doOtherAsyncStuff = async () => {
await saveAddresses();
await otherFunction();
// etc
};
```
### Serial vs parallel execution
`asyncForEach` resolves the callbacks in a serial fashion. This means that it waits until a callback is fully resolved before moving onto the next element in the list.
To launch all callbacks in parallel, you would have to do something like below instead:
```js
import { all } from 'rsvp';
const saveAddressesParallel = async () => {
await all(person.get('addresses').toArray().map(async (address) => {
const address = await person.get('address');
if (address.get('isDirty')) {
return await address.save();
}
}));
};
```
Contributing
------------------------------------------------------------------------------
See the [Contributing](CONTRIBUTING.md) guide for details.
License
------------------------------------------------------------------------------
This project is licensed under the [MIT License](LICENSE.md).