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

[feature request] alterItems allows async func? #307

Closed
beeplin opened this issue Dec 6, 2017 · 3 comments
Closed

[feature request] alterItems allows async func? #307

beeplin opened this issue Dec 6, 2017 · 3 comments

Comments

@beeplin
Copy link
Contributor

beeplin commented Dec 6, 2017

Now alterItems only works for sync func. Sometimes we need another alterItemsAsync to handle async funcs.

@eddyystop
Copy link
Collaborator

eddyystop commented Dec 6, 2017

Could an IIFE async/await be used?

alterItems(rec => {
  rec.userRecord = (async () => await service.get(...) )()
})

The problem with supporting an async func is that the hook is designed to modify the data/result object in place, so the return value is ignored.

We could have a spurious return value. If that is a Promise, we can wait on it to resolve and only then resolve the returned hook value.. Something similar to

return context => {
    const result = predicate(context);
    if (!result || typeof result.then !== 'function') {
      return !result;
    }

    return result.then(result1 => !result1);
  };

The only way I can see to return such a spurious Promise would be

alterItemsModified(rec => new Promise(resolve => {
  service.get(...).then(result => {
    rec.userRecord = result;
    resolve();
}});

Comments?

@eddyystop
Copy link
Collaborator

Handled by #313

@beeplin please let me know if there are any issues.

@eddyystop
Copy link
Collaborator

Documentation updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants