metarhia / metasync

Asynchronous Programming Library for JavaScript & Node.js
https://metarhia.com
MIT License
206 stars 35 forks source link

Add support for iterable #313

Open nechaido opened 6 years ago

nechaido commented 6 years ago

I propose to add support for everything that implements iterable or iterator protocols to such methods as:

Also, it may be useful for these functions to support objects that doesn't implement any of the above protocols, this way they will be iterated over pairs of [key, value].

tshemsedinov commented 6 years ago

@nechaido what is the difference between .for and this .each ?

nechaido commented 6 years ago

@tshemsedinov for is used to create an ArrayChain.

nechaido commented 6 years ago

@tshemsedinov here are some examples. metasync.for:

const set = new Set([1, 2, 3, 4]);

metasync
  .for(set)
  .map((item, callback) => callback(null, item * 2))
  .reduce((previous, current, callback) => callback(null, previous + current))
  .fetch((error, result) => {
    if (error) {
      console.error(error);
      return;
    }
    console.log(result);
  });

metasync.each:

const set = new Set([1, 2, 3, 4]);

metasync.each(
  set,
  (item, callback) => {
    console.log(item * 2);
    callback(null);
  },
  (error) => {
    if (error) {
      console.error(error);
    }
  }
);