1.0.2 • Published 11 months ago

ts-coroutines v1.0.2

Weekly downloads
-
License
ISC
Repository
github
Last release
11 months ago

TypeScript Coroutines

This package provides a coroutine function that allows you to write asynchronous code in a synchronous manner, using TypeScript's Generator functions.

Installation

You can install ts-coroutines with npm:

npm i ts-coroutines

Usage

First, import the coroutine function from the ts-coroutines package:

import { coroutine } from 'ts-coroutines';

Then, create a generator function where yield is used to pause and resume the execution:

const result = coroutine(function* () {
  const response = yield fetch('https://api.example.com');
  const data = yield response.json();
  return data;
});

The coroutine function returns a promise that resolves with the value returned from the generator function.

Error Handling

Errors can be handled inside the generator function with a try-catch block:

coroutine(function* () {
    try {
        const result = yield new Promise((_, reject) => setTimeout(() => reject('Error'), 500));
        console.log(result);
    } catch (error) {
        console.error(error);  // 'Error'
    }
});

If an error is thrown and not caught inside the generator function, it will reject the promise returned by the coroutine function.

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago