async-any v1.0.0
async-any
NAME
async-any - manage various forms of asynchronous completion in a uniform way
INSTALLATION
$ npm install async-any
SYNOPSIS
import asyncAny from 'async-any'
// async task taking a `done` callback
asyncAny(done => fs.stat(path, done), (error, result) => { ... })
// async task returning a promise or observable
asyncAny(() => fetch(url), (error, result) => { ... })
// returns a promise if the callback is omitted
let result = await asyncAny(task)
DESCRIPTION
This module exports a function which provides a uniform way to handle tasks which signal completion asynchronously — either by calling a callback, returning a promise, or returning an observable.
Why?
I wanted a lightweight version of async-done without the bugs, with stricter detection of promises and observables, with an optional promise API, and with browser support.
Why Not?
async-any doesn't support event emitters, ChildProcess or other kinds of Node.js streams (unless they also happen to be promises or observables). If you need support for these, use async-done.
EXPORTS
asyncAny (default)
Signature:
- asyncAny(task: (done: Errback) ⇒ Promise|Observable|void, callback: Errback) ⇒ void
- asyncAny(task: (done: Errback) ⇒ Promise|Observable|void) ⇒ Promise
function runTask (task) {
asyncAny(task, (error, result) => {
if (!error) console.log('got result:', result)
})
}
// or
async function runTask (task) {
let result = await asyncAny(task)
console.log('got result:', result)
}
Takes an asynchronous task (function) and a callback. The task is passed a done
"errorback"
function which can be used to signal completion. Alternatively, the task can return
a promise or an observable and will be deemed complete when that "future"
succeeds or fails.
Once the task has completed, its error/result is forwarded to the callback. If the callback is omitted, a promise is returned, which is fulfilled/rejected by the corresponding result/error.
If the task returns a value which is both a promise and an observable, it is treated as a promise.
DEVELOPMENT
NPM Scripts
The following NPM scripts are available:
- test - lint the codebase, compile the library, and run the test suite
Gulp Tasks
The following Gulp tasks are available:
- build - compile the library and save it to the target directory
- clean - remove the target directory and its contents
- default - run the
lint
andbuild
tasks - dump:config - print the build config settings to the console
- lint - check and report style and usage errors in the gulpfile, source file(s) and test file(s)
SEE ALSO
VERSION
1.0.0
AUTHOR
COPYRIGHT AND LICENSE
Copyright © 2018 by chocolateboy.
This is free software; you can redistribute it and/or modify it under the terms of the Artistic License 2.0.