metasync icon indicating copy to clipboard operation
metasync copied to clipboard

support Iterable objects, such as Map, Set, String

Open KHYehor opened this issue 5 years ago • 7 comments

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

KHYehor avatar Mar 17 '19 19:03 KHYehor

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

o-rumiantsev avatar Mar 18 '19 08:03 o-rumiantsev

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

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

o-rumiantsev avatar Mar 19 '19 07:03 o-rumiantsev

@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 avatar Mar 20 '19 15:03 KHYehor

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 avatar Mar 24 '19 19:03 KHYehor

@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 avatar Mar 25 '19 14:03 KHYehor

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

KHYehor avatar Mar 29 '19 13:03 KHYehor

ping @belochub.

nechaido avatar Jun 26 '19 13:06 nechaido