metarhia / metasync

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

Implement .reduce(items, callback, done, initial) #17

Closed tshemsedinov closed 7 years ago

tshemsedinov commented 7 years ago

Use case:

metasync.reduce(['Beijing', 'Roma', 'Kiev'], (prev, cur, callback) => {
  getCityPopulation(cur, (count) => {
    callback(prev + count);
  });
}, (err, count) => {
  console.log('Total population: ' + count);
}, 0);
anxolerd commented 7 years ago

@tshemsedinov would you kindly update the use case, so it matches the signature provided in the topic? TIA

UPD: not need =/

aqrln commented 7 years ago

@anxolerd doesn't it match?

anxolerd commented 7 years ago

@aqrln did not notice the initial and get lost in callback function =) Sorry)

aqrln commented 7 years ago

@anxolerd np :)

aqrln commented 7 years ago

@anxolerd thanks for your feedback, though. The fact that you could not read the code snippet correctly clearly indicates that the syntax may not be that great (and it indeed isn't; things that work well in non-asynchronous world may become way too complicated when asynchronous operations are being involved). This one and accompanying functions like metasync.filter and metasync.map will surely exist mimicking Array.prototype.*, but we came up with an idea of much more readable and convenient alternative mechanism for applying such transformations, chainable and Promise-friendly. Stay tuned :)

tshemsedinov commented 7 years ago

What we certainly have... a confusing syntax ) I propose add alternative syntax:

metasync.for(items).reduce(fn).done(fn);
// or
metasync.for(items).reduce(fn).then(fn).catch(fn);
anxolerd commented 7 years ago

Well, it was a surprise to find out that reduce is already implemented. Probably the alternate chainable and promise friendly syntax should be moved to a new issue (if it does not exist), and this one should be closed in order to not confuse others...

tshemsedinov commented 7 years ago

See #37

aqrln commented 7 years ago

@anxolerd see https://github.com/metarhia/MetaSync/pull/39 and an example of usage.