metarhia / metasync

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

Unpredictable search result #442

Open mapogolions opened 4 years ago

mapogolions commented 4 years ago
metatests.test('unpredictable search result', test => {
  const data = [{ name: 'Bob', age: 34 }, { name: 'Bob', age: 45 }];
  const expected = 34; // actual search result - 45
  const predicate = msec => {
    let delay = true;
    return (item, callback) => {
      if (delay) {
        delay = false;
        setTimeout(() => callback(null, item.name === 'Bob'), msec);
        return;
      }
      process.nextTick(() => callback(null, item.name === 'Bob'));
    };
  };

  metasync.find(data, predicate(200), (_, result) => {
    test.strictSame(result.age, expected);
    test.end();
  });
});

1) Copy & past above snippet to the test/array.find.js 2) Enter to the terminal npx metatests test/array.find.js In my opinion, method find should have sequential (series) semantics. Otherwise, we will obtain unpredictable search result depends on non deterministic delay of non-blocking operation