Alex A. Naanou 356df9c1d5 notes...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
2022-07-07 11:29:21 +03:00
2022-07-05 22:29:13 +03:00
2022-07-07 11:29:21 +03:00

stoppable.js

Utility library implementing tooling to make stoppable functions...

Install / import

$ npm install --save ig-stoppable
var stoppable = require('ig-stoppable')

Components

stoppable(..)

Create a wrapper for the input function/generator.

stoppable(<function>)
    -> <function>

stoppable(<async-function>)
    -> <async-function>

stoppable(<generator>)
    -> <generator>

stoppable(<async-generator>)
    -> <async-generator>

stoppable.STOP / stoppable.STOP(..)

A special object/constructor that can be either returned/thrown as-is or used to create an instance to be returned thrown.

stoppable.STOP

stoppable.STOP()
stoppable.STOP(<value>)
    -> <stop-object>

This will get intercepted by stoppable(..) and appropriately handled merging it into the return/yield value and stopping the function/iterator.

<stop-object> can contain a value that will get handled by stoppable(..) (default: undefined).

Examples

Function

var func = stoppable(function(){
    // ...

    throw stoppable.STOP('something')

    // ...
})

var value = func() // -> 'something'

Async function

var func = stoppable(async function(){
    // ...

    throw stoppable.STOP('something')

    // ...
})

var value = await func() // -> 'something'

Generator

var gen = stoppable(function*(){
    // ...

    throw stoppable.STOP('something')

    // ...
})

var value = [...gen()] // -> ['somthing']

Async generator

var agen = stoppable(async function*(){
    // ...

    throw stoppable.STOP('something')

    // ...
})

var value = awat agen() // -> ['something']

License

BSD 3-Clause License

Copyright (c) 2022-, Alex A. Naanou,
All rights reserved.

Description
Utility library implementing tooling to make stoppable functions...
Readme BSD-3-Clause 41 KiB
Languages
JavaScript 100%