Effect CommunityEC
Effect Community3y ago
6 replies
kristo

Short-circuiting a pipeline based on predicate

Is there any way to short-circuit a pipeline given a condition? I have a function to refresh credentials, but if the creds aren't expiring I want to short circuit my pipeline and return the credentials early:

const verifyCredentials = () =>
  pipe(
    getCredentials,
    Effect.succeed(creds),
    Effect.filterOrElse(
      isExpiring,
      creds => Effect.succeed(creds) // here I want to simply return the original credentials
    ),
    Effect.map(creds => {
      const req = new Request(oauthRefreshUrl);
      req.headers.set("Authorization", creds.refreshToken);
    }),
    tryFetch,
    checkResponseOk,
    parseJson,
    setCredentials
  );


This would be very easy with generators, just return early. But how would I accomplish the same with pipe?
Was this page helpful?