npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

aico

v1.0.1

Published

abort in coroutines (promise)

Downloads

14

Readme

A.I.C.O 🦄

Abort In COroutines (promise)

npm npm

aico was inspired by redux-saga's Task cancellation. I wanted to use it in promises and found several alternatives. But they are a little bit verbose or lacking. aico writes less and does more. And it supports AbortController and typescript.

aico

(I enjoyed watching A.I.C.O on Netflix)

Example

import { aico } from 'aico'

const promise = aico(function* (signal) {
    try {
        yield fetch('/delay/100', { signal }) // <= This api takes 100ms.
        console.log('1. This is printed.')

        yield fetch('/delay/100', { signal }) // <= This api takes 100ms.
        console.log('2. This is not printed.')
    } finally {
        if (signal.aborted) {
            console.log('3. aborted!')
        }
    }
})

promise.catch(err => {
    console.log(`4. message: ${err.name}`)
    console.log(`5. isAborted: ${err.isAborted}`)
})

setTimeout(() => {
    promise.abort() // <= After 150ms
}, 150)
> output
1. This is printed.
3. aborted!
4. message: AbortError
5. isAborted: true

Install

npm install aico

API

new AbortInCoroutines(generator, options?)

Create an abortable promise using a generator. In a generator, yield is the same as async function's await.

import { AbortInCoroutines } from 'aico'

const promise = new AbortInCoroutines(function* (signal) {
    const result = yield Promise.resolve('hello') // <= result is "hello".

    return result
})

promise.then(val => {
    console.log(val) // => "hello"
})

signal parameter is AbortSignal that can cancel DOM requests such as fetch.

const promise = new AbortInCoroutines(function* (signal) {
    const response = yield fetch('/api/request', { signal })

    console.log('This is not printed.')
})

promise.abort() // <= Abort `/api/request` request.

signal has an aborted property that indicates whether the promise was aborted or not.

const promise = new AbortInCoroutines(function* (signal) {
    try {
        /* ... */
    } finally {
        if (signal.aborted) {
            console.log('aborted!')
        } else {
            console.log('not aborted.')
        }
    }
})

promise.abort() // => "aborted!"

If the yielded promise was created with AbortInCoroutines, the cancellation is propagated.

const subTask = () =>
    new AbortInCoroutines(function* (signal) {
        try {
            /* ... */
        } finally {
            if (signal.aborted) {
                console.log('subTask is aborted!')
            }
        }
    })

const promise = new AbortInCoroutines(function* () {
    yield subTask()
})

promise.abort() // => "subTask is aborted!"

options

signal

This is an option to abort a promise with the signal of the external controller.

const controller = new AbortController()

const promise = new AbortInCoroutines(
    function* (signal) {
        try {
            /* ... */
        } finally {
            if (signal.aborted) {
                console.log('aborted!')
            }
        }
    },
    {
        signal: controller.signal, // 👈 Here, the external controller's signal is used.
    }
)

controller.abort() // => aborted!
unhandledRejection

If there is no catch handler registered when this is true, an unhandledRejection occurs. Default is false.

new AbortInCoroutines(
    function* () {
        /* ... */
    },
    {
        unhandledRejection: true,
    }
).abort()
// => `unhandledRejection` warning is printed.

promise.isAborted

Returns whether the promise is aborted or not.

const promise = new AbortInCoroutines(/* ... */)

console.log(promise.isAborted) // => false

promise.abort()

console.log(promise.isAborted) // => true

promise.abort()

Abort the promise.

aico(generator, options?)

This function can be used instead of the verbose new AbortInCoroutines().

import { aico } from 'aico'

const promise = aico(function* (signal) {
    /* ... */
})

Combinators

all(values)

This is an abortable Promise.all().

import { aico, all } from 'aico'

const fetchData = url =>
    aico(function* (signal) {
        try {
            /* ... */
        } finally {
            if (signal.aborted) {
                console.log(`aborted : ${url}`)
            }
        }
    })

const promise = all([fetchData('/api/1'), fetchData('/api/2'), fetchData('/api/3')])

promise.abort()
// => aborted : /api/1
// => aborted : /api/2
// => aborted : /api/3

If one is rejected, the other promise created by aico is automatically aborted.

const promise = all([fetchData('/api/1'), fetchData('/api/2'), fetchData('/api/3'), Promise.reject('fail')])
// (This is printed immediately)
// => aborted : /api/1
// => aborted : /api/2
// => aborted : /api/3

race(values)

This is an abortable Promise.race().

import { race } from 'aico'

const timeout = ms => new Promise((_, reject) => setTimeout(reject, ms))

const promise = race([
    fetchData('/delay/600'), // <= This api takes 600ms.
    timeout(500),
])

// (After 500ms)
// => aborted : /delay/600

Likewise, if one is rejected, the other promise created by aico is automatically aborted.

any(values)

This is an abortable Promise.any().

allSettled(values)

This is an abortable Promise.allSettled().

cast(promise)

In TypeScript, type inference of yielded promise is difficult. So do type assertions explicitly.

import { aico, cast } from 'aico'

const promise = aico(function* () {
    const data = (yield asyncTask()) as { value: string }

    // or
    const data = (yield asyncTask()) as Awaited<ReturnType<typeof asyncTask>>
})

Or a cast function and yield* combination, type inference is possible without type assertion.

import { aico, cast } from 'aico'

const promise = aico(function* () {
    const data = yield* cast(asyncTask())
})

License

MIT © skt-t1-byungi