new AsyncAfWrapper(data) → {Object}
empty AsyncAF class wrapper
AsyncAfWrapper is one option for cherry-picking only the methods you'd like to use in your code; use, series, and io are the only methods initially available on AsyncAfWrapper; see example below
Note: while AsyncAfWrapper is a class, it can create instances with or without the new
keyword
Example
say you only want to use mapAF, filterAF, forEachAF, and logAF instead of pulling in the entire AsyncAF library
first, install the separate packages (e.g., for npm):
$ npm install --save @async-af/{wrapper,map,filter,foreach,log}
or, if on Windows:
$ npm install --save @async-af/wrapper @async-af/map @async-af/filter @async-af/foreach @async-af/log
then import the packages
import AsyncAF from '@async-af/wrapper'; // aliasing 'AsyncAfWrapper' as 'AsyncAF'
import mapAF from '@async-af/map';
import filterAF from '@async-af/filter';
import forEachAF from '@async-af/foreach';
import logAF from '@async-af/log';
if you'd like to save some vertical screen real estate and cut the imports down to one line, see Wrapper/Use: Too Many 🤬 Imports!?
then call use, including all prototype methods you'd like to add to AsyncAfWrapper's prototype in the first argument, prototypeMethods
and all static methods you'd like to add to AsyncAfWrapper in the second optional argument, staticMethods
AsyncAF.use({ // prototype methods go in the first argument
mapAF,
filterAF,
forEachAF
}, { // static methods go in the second argument
logAF
});
ready to go!
const promises = [1, 2, 3].map(n => Promise.resolve(n));
AsyncAF(promises).mapAF(n => n * 2).filterAF(n => n !== 4).forEachAF(n => console.log(n));
// logs 2 then 6
AsyncAF.logAF(promises);
// @filename.js:24:9:
// [ 1, 2, 3 ]
// in 0.003 secs
protip: you can use the same technique to add your own custom prototype or static methods to AsyncAfWrapper or even to the main AsyncAF class; see use for an example
- Since:
- 3.0.0
- See:
Parameters:
Name | Type | Description |
---|---|---|
data | any | the data to be wrapped by the AsyncAF class; can be promises or non-promises |
Returns:
returns an instance of AsyncAfWrapper wrapping the passed in data
- Type
- Object
Methods
(static) use(prototypeMethods, staticMethodsopt) → {undefined}
adds prototype/static methods to AsyncAF or AsyncAfWrapper
see AsyncAfWrapper for an example of how to cherry-pick AsyncAF methods you'd like to use rather than pulling in the entire AsyncAF library;
for something different, the following shows how to add custom methods to AsyncAF & AsyncAfWrapper
Example
say you want to extend AsyncAF with your own prototype method that acts on an array of numbers or promises that resolve to numbers and naively adds them up
let's call it sumAF; here's some code:
// sumAF.js
const sumAF = function () {
return this.then(nums => Promise.all(nums))
.then(nums => nums.reduce((sum, num) => sum + num));
};
export default sumAF;
pull in AsyncAF or AsyncAfWrapper and sumAF
to the file you'd like to use it in:
// otherFile.js
import AsyncAF from 'async-af'; // or import AsyncAF from '@async-af/wrapper';
import sumAF from './sumAF';
then, call use
on AsyncAF
and pass in sumAF
wrapped in an object to the first parameter, prototypeMethods
:
// otherFile.js
// ...
AsyncAF.use({sumAF});
ready! now your custom prototype method will be available on AsyncAF
// otherFile.js
// ...
const promises = [1, 2, 3].map(n => Promise.resolve(n));
const sum = AsyncAF(promises).sumAF()
AsyncAF.logAF(sum);
// @otherFile.js:10:9:
// 6
// in 0.001 secs
if you'd like to add a static method to AsyncAF, use
accepts a second optional argument staticMethods
; for example:
const staticNoop = () => {};
AsyncAF.use({}, {staticNoop});
AsyncAF.staticNoop(); // noop
- Since:
- 3.0.0
- See:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
prototypeMethods | Object | an Object containing the prototype methods you'd like to use | |
staticMethods | Object | <optional> | an Object containing the static methods you'd like to use |
Returns:
adds prototype/static methods to AsyncAF or AsyncAfWrapper
- Type
- undefined