metarhia / metasync

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

support Iterable objects, such as Map, Set, String #415

Open KHYehor opened 5 years ago

KHYehor commented 5 years ago

I added support for iterable objects using [Symbol.iterator], also tests are written

o-rumiantsev commented 5 years ago

It is better to use common.iter to iterate over the iterable.

o-rumiantsev commented 5 years ago

It is better to use common.iter to iterate over the iterable.

You can also use metasync.asyncIter instead of common.iter where needed

KHYehor commented 5 years ago

@o-rumiantsev I have one misunderstanding with you. It is said that Asynchronous map (iterate parallel) and there isn't used any async/await or promises, only call back next(). If I insert common.iter I will get the same that I had. If I insert metasync.iter it will be some different, and I also get strange error messages Message: Failure after end: callback is not a function. Or I just can't understand something.

KHYehor commented 5 years ago

Now I have one inaccuracy. If I call method .fetch() then call other methods like .filter(), .reduce(), ... I will get wrong behavior. Because string this.execute() is async, and I return this which isn't calculated yet. It could be solved with Promise or async/await but you might solute something else. test/chain.js 'for chain after fetch' I am talking about this one.

ArrayChain.prototype.fetch = function(fn) {
  this.chain.push({ op: 'fetch', fn });
  this.execute();
  return this;
};
KHYehor commented 5 years ago

@SemenchenkoVitaliy @belochub I noticed one interesting detail, I can be wrong so correct me if I am. If I rewrite metatest 'for chain after fetch' to process.nextTick like this

metatests.test('for chain after fetch', test => {
  metasync
    .for([1, 2, 3, 4])
    .map((item, cb) => process.nextTick(() => cb(null, item * item)))
    .filter((item, cb) => process.nextTick(() => cb(null, item > 5)))
    .fetch((error, result, resume) => {
      test.error(error);
      test.strictSame(result, [9, 16]);
      process.nextTick(() => resume(null, result));
    })
    .filter((item, cb) => {
      process.nextTick(() => cb(null, item > 10));
    })
    .map((item, cb) => {
      process.nextTick(() => cb(null, --item));
    })
    .fetch((error, result) => {
      test.error(error);
      test.strictSame(result, [15]);
      test.end();
    });
});

I will get async functions, but it doesn't work as should, I will get errors. (it is master) And If I rewrite all methods to asyncIter as I made, I will get the same errors with any fn. How should it be solute?

KHYehor commented 5 years ago

In last commit I have described my problem with lib/chain.js I am trying to find the solution for this. And you can check and say what you think about lib/array.js. P.S. Tests failed because of reserved words await and next() in v6.x and v8.x

nechaido commented 5 years ago

ping @belochub.