Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with promises #129

Closed
emahuni opened this issue Jun 10, 2022 · 2 comments
Closed

Working with promises #129

emahuni opened this issue Jun 10, 2022 · 2 comments

Comments

@emahuni
Copy link

emahuni commented Jun 10, 2022

I am trying to use some of these functions with promises and it doesn't work.

Any plans to support promises?

eg;

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

The await before _.eachDeep is trying to wait for all the promises. Problem is that each used value will end up being a promise, which goes back to the original problem of finding and awaiting each promise.

Is there a way to achieve this?

@emahuni
Copy link
Author

emahuni commented Jun 10, 2022

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.

@YuriGor
Copy link
Owner

YuriGor commented Jul 16, 2022

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.

@YuriGor YuriGor closed this as not planned Won't fix, can't repro, duplicate, stale Jul 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants