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

How do I get promise from the query? #484

Open
falkomerr opened this issue Apr 22, 2024 · 3 comments
Open

How do I get promise from the query? #484

falkomerr opened this issue Apr 22, 2024 · 3 comments
Labels
type:enhancement New feature or request

Comments

@falkomerr
Copy link

Promise can be useful to render queries in promise oriented frameworks such as svelte

Example:
image

@igorkamyshev igorkamyshev added the type:enhancement New feature or request label May 1, 2024
@igorkamyshev
Copy link
Owner

Use case form the chat:

image

@dovranJorayev
Copy link

dovranJorayev commented Aug 24, 2024

In most form management solutions, APIs outside the Effector ecosystem typically rely on promises rather than event-based APIs. I've observed this in the React ecosystem with the libraries listed below, and I believe the same holds true for Vue:

  • react-hook-form, @tanstack/react-form (@tanstack-form/core)
  • zod, including .refine for async validation
  • react-router-dom, specifically loaders for preloading route initial data
  • Notification libraries such as @mantine/notifications
  • Locally implemented libraries with promise-based APIs

To address this, I've developed an Effector operator that converts a remote operation (e.g., a query) into an Effector effect. This operator injects additional metadata into the query call parameters, requiring object constraints for these parameters.

/**
 * Converts a Farfetched Query or Mutation instance to an Effector effect.
 * @template Params - Operation parameters with Record<string, unknown> constraints.
 * @template Data - Operation success data.
 * @param operation - Farfetched Query or Mutation instance.
 * @returns An effect with parameters matching the operation's start event.
 *
 * @throws {OperationAbortError} If the operation was aborted.
 * @throws {OperationSkipError} If the operation was skipped.
 * @throws {Error} If the parameters are not an object.
 */
function toEffect<Params extends Record<string, unknown>, Data, Err>(
  operation: Query<Params, Data, Err> | Mutation<Params, Data, Err>
): Effect<Params, Data, Error>;

export class OperationSkipError extends Error {}
export class OperationAbortError extends Error {}

@dovranJorayev
Copy link

I have covered operator with unit tests to check

  • work correctness with plain query success, fail, skip, abort cases
  • params object constraints
  • work correctness with enabled option
  • work correctness with concurrency operator and it's strategies
  • work correct with cache adapter

It is working good and cover my cases at the moment, but have such operator in the farfetched will be very helpful for others + will cover operations with void params

implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants