sift.js icon indicating copy to clipboard operation
sift.js copied to clipboard

Support asynchronous (promised-based) custom matchers

Open bradvogel opened this issue 7 years ago • 2 comments

For our use case, we'd like to implement custom expressions that make database calls. An example of what we'd like to do (that doesn't work today):

sift.use({
  $existsInDb: async function(expectToExist, name) {
    const existsInDb = !!(await db.users.findOne({ name }));
    return existsInDb === expectToExist;
  }
});

const result = await sift({ $existsInDb: true }, ['bob', 'ben', 'brad']);

bradvogel avatar Mar 04 '18 20:03 bradvogel

FYI: we forked sift.js and implemented async support in https://github.com/mixmaxhq/sift.js/pull/1.

skeggse avatar Apr 11 '19 22:04 skeggse

Just starting on this functionality in #157. The API will likely be:

const asyncFilter = sift({ $existsInDb: true }, {
  operations: {
    async $existsInDb(expectToExist, name) {
      const existsInDb = Boolean(await db.users.findOne({ name }));
      return existsInDb === expectToExist;
    }
  }
});

const results = await Promise.all(['bob', 'ben', 'brad'].filter(asyncFilter));

crcn avatar Jun 23 '19 21:06 crcn