mirror of
https://github.com/flynx/stoppable.js.git
synced 2025-10-28 10:20:10 +00: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
Copyright (c) 2022-, Alex A. Naanou,
All rights reserved.
Description
Languages
JavaScript
100%