Git Product home page Git Product logo

Comments (2)

emahuni avatar emahuni commented on May 27, 2024

So what I have done is this:

const vows = [];
_.eachDeep(filters, (val, key, parent, ctx) => {
   // ...
  vows.push(someAsyncFn(val).then(res=>parent[key] = res);
}, { leavesOnly: true, checkCircular: true });
await Promise.all(vows)

I wish this could be done internally. You see the ...deep functions just have to detect if the callback is an async function, and do exactly like above before returning the modified obj. They do the then and setting of the parent[key].

Detection of async function and vow keeping can be achieved like so:

 if (cb instanceof Object.getPrototypeOf(async function () {}).constructor) {
    vows.push(cb(value, key, parent, ctx).then(res => parent[key] = res));
  } else {
    parent[key] = cb(value, key, parent, ctx); // normal callback function
  }
// ... then finally, before we return everything we return a promise that awaits the vows, but resolves to the object
return Promise.all(vows).then(res=>originalObjWeWereIterating)

That will allow the functions to be used like so:

filters = await _.eachDeep(filters, async (val, key, parent, ctx) => {
   // ...
  return parent[key] = await someAsyncFn(val);
}, { leavesOnly: true, checkCircular: true });

it won't change semantics, but enhance the lib to work with promises.

from deepdash.

YuriGor avatar YuriGor commented on May 27, 2024

Hi @emahuni - your implementation is 100% valid and if I would need to do it I would do the same.

Still it makes no sense to support it in deepdash internally: rare use case, but performance will be affected in all cases.

from deepdash.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.