fp-ts-timeout
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

fp-ts-timeout

Timeout functionality for fp-ts async structures.

Build Status npm version

Installation

npm install fp-ts-timeout

Requirements

  • Node.js v.18+
  • TypeScript v.4.5+

API

withTaskEitherTimeout

A higher-order function for setting a timeout on asynchronous operations.

Example

import * as Either from 'fp-ts/Either';
import * as TaskEither from 'fp-ts/TaskEither';
import { withTaskEitherTimeout, isTimeoutError } from 'fp-ts-timeout';

pipe(
  TaskEither.tryCatch(
    () => {
      // some async operation
    }),
    Either.toError
  ),
  withTaskEitherTimeout(1000) // 1 second timeout
  TaskEither.match(
    (error) => {
      if (isTimeoutError(error)) {
        // handle timeout error
      } else {
        // handle other errors
      }
    },
    (result) => {
      // handle success
    }
)

withReaderTaskEitherTimeout

A higher-order function for setting a timeout on asynchronous operations.

Example

import * as Either from 'fp-ts/Either';
import * as TaskEither from 'fp-ts/TaskEither';
import * as ReaderTaskEither from 'fp-ts/ReaderTaskEither';
import { withTaskEitherTimeout, isTimeoutError } from 'fp-ts-timeout';

pipe(
  TaskEither.tryCatch(
    () => {
      // some async operation
    }),
    Either.toError
  ),
  ReaderTaskEither.fromTaskEither,
  withReaderTaskEitherTimeout(1000), // 1 second timeout
  TaskEither.match(
    (error) => {
      if (isTimeoutError(error)) {
        // handle timeout error
      } else {
        // handle other errors
      }
    },
    (result) => {
      // handle success
    }
)

TimeoutError

Custom error thrown when an asynchronous operation exceeds the defined timeout limit.

withTaskEitherRetry

A higher-order function for retrying asynchronous operations, utilizing p-retry under the hood. Refer to the package documentation for available options and details.

Example

import * as Either from 'fp-ts/Either';
import * as TaskEither from 'fp-ts/TaskEither';
import { withTaskEitherRetry } from 'fp-ts-timeout';

pipe(
  TaskEither.tryCatch(
    () => {
      // some async operation
    }),
    Either.toError
  ),
  withTaskEitherRetry({ retries: 2 }),
  TaskEither.match(
    (error) => {
      // handle errors
    },
    (result) => {
      // handle success
    }
)

withReaderTaskEitherRetry

A higher-order function for retrying asynchronous operations, utilizing p-retry under the hood. Refer to the package documentation for available options and details.

Example

import * as Either from 'fp-ts/Either';
import * as TaskEither from 'fp-ts/TaskEither';
import * as ReaderTaskEither from 'fp-ts/ReaderTaskEither';
import { withReaderTaskEitherRetry } from 'fp-ts-timeout';

pipe(
  TaskEither.tryCatch(
    () => {
      // some async operation
    }),
    Either.toError
  ),
  ReaderTaskEither.fromTaskEither,
  withReaderTaskEitherRetry({ retries: 2 }),
  TaskEither.match(
    (error) => {
      // handle errors
    },
    (result) => {
      // handle success
    }
)

Contribute

Source code contributions are most welcome. Please open a PR, ensure the linter is satisfied and all tests pass.

We are hiring

Causaly is building the world's largest biomedical knowledge platform, using technologies such as TypeScript, React and Node.js. Find out more about our openings at https://apply.workable.com/causaly/.

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i fp-ts-timeout

Weekly Downloads

529

Version

1.0.0

License

MIT

Unpacked Size

18.1 kB

Total Files

6

Last publish

Collaborators

  • jmike
  • dnlytras
  • nikoskalogridis