npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-cli-sync-for-each

v1.0.8

Published

Synchronously calls a function on each item of an array. If that function is asynchronous, it will wait for the previous call to resolve before triggering the next.

Downloads

4

Readme

Ember-cli-sync-for-each

Build Status Code Climate

This addon provides you with a function to execute asynchronous callbacks synchronously on an array of objects. This is for example usefull if you want to save a list of models where each model depends on data that is returned by the backend for one of the previous models (such as the id). Or when you want to mass create a list of objects but also respect their order of creation for the timestamps generated by your backend. For more use cases see the guide section below.

API

syncForEach(array, callback, force, index);

| Parameter | Description | | ------------- |:-----| | array | List of items for which the callback is going to be executed. | | callback | Function that is going to be executed synchronously for every item. | | | signature: (item, index, array) | | force | If set to true, execution will not stop should the callback reject for one item. | | index | Specifies at what position the function should start traversing the array. (used internally) |

Guide

simple usage

When your user is able to mass create a list of todos and you retroactively want to commit them to your backend, varying network roundtrip times can mess with their order of creation. For most use cases this is fine, but sometimes your application depends on respecting that order. You can use syncForEach to throttle the POST requests in a way that every request will only be sent once the previous request returned successfully.

import syncForEach from 'ember-cli-sync-for-each';

syncForEach(model.get('todos'), function(todo) {
  return todo.save();
});

access enumerable

The callback additionally gives you access to the enumerable. You can use syncForEach therefor, to access resolved data from previous calls. In the following example we want to commit a list of new employees to our company. There is a policy that every new employee will be mentored by the last hired coworker. Since syncForEach will already have successfully saved all previous coworkers they will already have ids set by the backend that you can reference.

syncForEach(employees, function(employee, index, employees) {
  previousEmployee = index === 0 ? null : employees[index-1];
  employee.set('mentor', previousEmployee);
  return employee.save();
});

resolves upon completion

Since syncForEach will either resolve once it executed the callback for every item or rejects as soon as its callback rejects for one of the items, you can use it to asynchronously run code in either event.

syncForEach(model.get('todos'), function(todo) {
  return todo.save();
}).then(function(todos){
  alert('The backend created todos with the following ids: ', todos.mapBy('id'));
});

optionally resumes on rejection

syncForEachs default behaviour is to stop execution once one callback rejects, so the following code:

syncForEach([1,2,3,4,5], function(item) {
  return new Ember.RSVP.Promise(function(resolve, reject){
    if (item === 3) {
      reject();
    } else {
      console.log(item);
      resolve();
    }
  });
});

will produce this output:

> 1
> 2

If you want the function to continue no matter what, you can set the optionally force flag to true. The promise that will be returned from syncForEach will still reject with the respective rejection message but your callback will be executed for the remaining items anyway.

promise = syncForEach([1,2,3,4,5], function(item) {
  return new Ember.RSVP.Promise(function(resolve, reject){
    if (item === 3) {
      reject();
    } else {
      console.log(item);
      resolve();
    }
  });
}, true);

promse.then(function() {
  console.log('yay =D');
}, function() {
  console.log('nay D=');
});

will produce the following output:

> 1
> 2
> 4
> 5
> nay D=

synchronous callback

syncForEach is smart enough to recognize wether your callback actually returns a promise and will behave like a normal forEach in case it does not. The function will still return a resolved promise in this case. So the following snippet:

syncForEach(['foo', 'bar', 'baz'], function(word) {
  console.log(word);
});

will output:

> foo
> bar
> baz

indicate loading

Imagine you need to load a bunch of different models with so much data, that you want to display some kind of loading indicator. With syncForEach you can display a loading message to the user while loading your model data one after another, allways updating the message after each successfull load.

_this = this;
syncForEach(['account', 'user', 'report', 'statistic'], function(model) {
  $('#loading-indicator').html('loading ' + model + 's');
  _this.store.find(model);
});

Installation

To use this addon in your project, just type:

$ ember install:addon ember-cli-sync-for-each

or for older versions of ember-cli (pre 1.4.0):

$ npm install --save-dev ember-cli-sync-for-each

and then import the function wherever you need it:

import syncForEach from 'ember-cli-sync-for-each';

Contributing

Im happy about everyone that wants to contribute, even opening an issue on github. However if you want to contribute to the code just follow the setup instructions below. If you are scared, since you've never contributed to open source projects before, you are in luck! This is a pretty simple addon. Just have a look into the implementation, and unit tests, that's all there is to it.

  • git clone https://github.com/lazybensch/ember-cli-sync-for-each
  • cd ember-cli-sync-for-each
  • npm install
  • bower install
  • ember test