AsyncAF

AsyncAF

new AsyncAF(data) → {Object}

class that holds all the AsyncAF methods

while AsyncAF is a class, it can create instances with or without the new keyword

Since:
  • 3.0.0
See:
Example
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
Parameters:
NameTypeDescription
dataany

the data to be wrapped by the AsyncAF class; can be promises or non-promises

Returns:

returns an instance of AsyncAF wrapping the passed in data

Type
Object

Methods

concatAF(…values) → {Promise.<Array>|Promise.<String>}

merges an array or string with one or more arrays, strings, or other values, and resolves to a new array or string;

concatAF flexibly accepts arrays, strings, promises, other values, or other instances of AsyncAF; see examples

Since:
  • 5.3.0
See:
  • concat (alias)
Example
// arrays
const arr = Promise.resolve([1, 2]);
AsyncAF(arr).concatAF([3, 4]); // Promise that resolves to [1, 2, 3, 4]
AsyncAF(arr).concatAF([3, 4], [5, 6]); // Promise that resolves to [1, 2, 3, 4, 5, 6]

// nested arrays
const arr1 = Promise.resolve([1, 2]);
const arr2 = [3, [4, 5]];
AsyncAF(arr1).concatAF(arr2); // Promise that resolves to [1, 2, 3, [4, 5]]

// strings
const str = Promise.resolve('str');
AsyncAF(str).concatAF('ing'); // Promise that resolves to 'string'
AsyncAF(str).concatAF('ing', 'y'); // Promise that resolves to 'stringy'

// other instances of AsyncAF
const aaf1 = AsyncAF([1, 2]);
const aaf2 = AsyncAF(3);

aaf1.concatAF(aaf2); // Promise that resolves to [1, 2, 3];

const aaf3 = AsyncAF('stringy');
const aaf4 = AsyncAF('AF');

aaf3.concatAF(aaf4); // Promise that resolves to 'stringyAF'

// promises
const [p1, p2, p3] = [[1, 2], 3, [4, [5, 6]]].map(v => Promise.resolve(v));

AsyncAF(p1).concatAF(p2); // Promise that resolves to [1, 2, 3]
AsyncAF(p1).concatAF(p2, p3) // Promise that resolves to [1, 2, 3, 4, [5, 6]]

const pStr1 = Promise.resolve('str');
const pStr2 = Promise.resolve('ing');
const pStr3 = Promise.resolve('y');

AsyncAF(pStr1).concatAF(pStr2); // Promise that resolves to 'string'
AsyncAF(pStr1).concatAF(pStr2, pStr3); // Promise that resolves to 'stringy'

// Note: concatAF will not resolve deeply nested promises; if you need this behavior, concatAF can be used in a
// function; for example, this function recursively flattens an array of promises

const flattenAsync = arr => AsyncAF(arr).reduceAF(async (acc, val) => {
  return Array.isArray(await val)
    ? AsyncAF(acc).concatAF(flattenAsync(val))
    : AsyncAF(acc).concatAF(val), []);
};
Parameters:
NameTypeAttributesDescription
valuesany<repeatable>

arrays, strings, or values to concatenate into a new array or string

  • if any values are a Promise, they'll first be resolved then concatenated
  • if any values are an instance of AsyncAF, they'll be merged into one instance
Returns:

Promise that resolves to a newly merged array or string

Type
Promise.<Array> | Promise.<String>

everyAF(callback, thisArgopt) → {Promise.<Boolean>}

tests whether all elements in the array pass the test implemented by the provided callback function

if any elements are a Promise, they will first be resolved in parallel and then tested

Note: since everyAF is run in parallel, callback will be run on all elements even if one of the first few elements fails the test; if this behavior is not desireable, consider using series.everyAF or its alias, io.everyAF

Since:
  • 3.2.0
See:
Example
const promises = [1, 2, 3].map(n => Promise.resolve(n));


// basic usage
const allAreOdd = AsyncAF(promises).everyAF(n => n % 2);

console.log(allAreOdd); // Promise that resolves to false

AsyncAF.logAF(allAreOdd); // logs false


// using .then
AsyncAF(promises).everyAF(n => n % 2).then(allAreOdd => {
  console.log(allAreOdd); // logs false
});


// inside an async function
(async () => {
  const allAreNums = await AsyncAF(promises).everyAF(
    n => typeof n === 'number'
  );
  console.log(allAreNums); // logs true
})();
Parameters:
NameTypeAttributesDescription
callbackFunction

function that tests each element of the array

callback accepts three arguments:

  • currentValue value of the current element being processed in the array
  • index(optional) index of currentValue in the array
  • array(optional) the array that everyAF is being applied to
thisArgObject<optional>

value to use as this when executing callback

Returns:

Promise that resolves to true if the callback function returns a truthy value for every array element; otherwise, false

Type
Promise.<Boolean>

filterAF(callback, thisArgopt) → {Promise.<Array>}

creates a new Array with all elements that pass the test implemented by the provided callback function

if any elements are a Promise, they will first be resolved in parallel and then tested

Note: if you'd rather resolve and test elements in series, consider using series.filterAF or its alias, io.filterAF

Since:
  • 3.0.0
See:
Example
const promises = [1, 2, 3].map(n => Promise.resolve(n));


// basic usage
const odds = AsyncAF(promises).filterAF(n => n % 2);

console.log(odds); // Promise that resolves to [1, 3]

AsyncAF.logAF(odds); // logs [1, 3]


// using .then
AsyncAF(promises).filterAF(n => n % 2).then(odds => {
  console.log(odds); // logs [1, 3]
});


// inside an async function
(async () => {
  const odds = await AsyncAF(promises).filterAF(
    n => n % 2
  );
  console.log(odds); // logs [1, 3]
})();
Parameters:
NameTypeAttributesDescription
callbackFunction

function that tests each element of the array; return true to keep the element, false to filter it out

callback accepts three arguments:

  • currentValue value of the current element being processed in the array
  • index(optional) index of currentValue in the array
  • array(optional) the array that filterAF is being applied to
thisArgObject<optional>

value to use as this when executing callback

Returns:

Promise that resolves to a new Array with the elements that pass the test; if no elements pass the test, the promise will resolve to an empty array

Type
Promise.<Array>

findAF(callback, thisArgopt) → {Promise.<any>}

resolves to the value of the first element in the array that satisfies the provided callback function; otherwise, undefined

Note: since findAF is run in parallel, callback will be run on all elements even if one of the first few elements passes the test; if this behavior is not desireable, consider using series.findAF or its alias, io.findAF

Since:
  • 3.5.0
See:
Example
const inventory = [
 {name: 'nuts', quantity: 2000},
 {name: 'bolts', quantity: 5000},
 {name: 'screws', quantity: 9001}
].map(part => Promise.resolve(part));

AsyncAF(inventory).findAF(part => part.name === 'screws');
// Promise that resolves to {name: 'screws', quantity: 9001}
Parameters:
NameTypeAttributesDescription
callbackFunction

function to test each element in the array

callback accepts three arguments:

  • currentValue value of the current element being processed in the array
  • index(optional) index of currentValue in the array
  • array(optional) the array that findAF is being applied to
thisArgObject<optional>

value to use as this when executing callback

Returns:

Promise that resolves to the first element in the array that passes the test; otherwise, undefined

Type
Promise.<any>

findIndexAF(callback, thisArgopt) → {Promise.<Number>}

resolves to the index of the first element in the array that satisfies the provided callback function; otherwise, -1

Note: since findIndexAF is run in parallel, callback will be run on all indices even if one of the first few indices passes the test; if this behavior is not desireable, consider using series.findIndexAF or its alias, io.findIndexAF

Since:
  • 3.5.0
See:
Example
const inventory = [
 {name: 'nuts', quantity: 2000},
 {name: 'bolts', quantity: 5000},
 {name: 'screws', quantity: 9001}
].map(part => Promise.resolve(part));

AsyncAF(inventory).findIndexAF(part => part.name === 'screws');
// Promise that resolves to 2
Parameters:
NameTypeAttributesDescription
callbackFunction

function to test each element in the array

callback accepts three arguments:

  • currentValue value of the current element being processed in the array
  • index(optional) index of currentValue in the array
  • array(optional) the array that findIndexAF is being applied to
thisArgObject<optional>

value to use as this when executing callback

Returns:

Promise that resolves to the index of the first element in the array that passes the test; otherwise, -1

Type
Promise.<Number>

forEachAF(callback, thisArgopt) → {Promise.<undefined>}

executes a callback function on each element in an array

if any elements are a Promise, they will first be resolved in parallel and then processed

Note: if you'd rather resolve and process elements in series, consider using series.forEachAF or its alias, io.forEachAF

Since:
  • 3.0.0
See:
Example
const promises = [1, 2].map(n => Promise.resolve(n));


AsyncAF(promises).forEachAF(el => {
  console.log(el); // logs 1 then 2
});
Parameters:
NameTypeAttributesDescription
callbackFunction

function to execute for each element

callback accepts three arguments:

  • currentValue value of the current element being processed in the array
  • index(optional) index of currentValue in the array
  • array(optional) the array that forEachAF is being applied to
thisArgObject<optional>

value to use as this when executing callback

Returns:

Promise that resolves to undefined

Type
Promise.<undefined>

includesAF(searchItem, fromIndexopt) → {Promise.<Boolean>}

determines whether an array, string, or array-like object includes a certain element or string, returning true or false as appropriate

Note: when called on an array or array-like object, includesAF is run in parallel and all elements will be resolved even if one of the first few elements is a match; if this behavior is not desireable, consider using series.includesAF or its alias, io.includesAF

Since:
  • 3.4.0
See:
Example
// includesAF on an array of promises
const nums = [1, 2, 3].map(n => Promise.resolve(n));

AsyncAF(nums).includesAF(2); // Promise that resolves to true

AsyncAF(nums).includesAF(5); // Promise that resolves to false

AsyncAF(nums).includesAF(1, 1); // Promise that resolves to false

AsyncAF(nums).includesAF(3, -1); // Promise that resolves to true

// includesAF on a promise-wrapped string
const string = Promise.resolve('test string');

AsyncAF(string).includesAF('test'); // Promise that resolves to true

AsyncAF(string).includesAF('nope'); // Promise that resolves to false

AsyncAF(string).includesAF('test', 5); // Promise that resolves to false

AsyncAF(string).includesAF('string', -6); // Promise that resolves to true

// includesAF on an array-like object
(async function () {
  if (await AsyncAF(arguments).includesAF(2)) {
    console.log('2 is included');
  }
})(1, 2, 3); // logs '2 is included'
Parameters:
NameTypeAttributesDefaultDescription
searchItemany

the element or string to search for

fromIndexNumber<optional>
0

the index at which to begin searching for searchItem; a negative value searches from the index of array/string.length - fromIndex; defaults to 0

Returns:

Promise that resolves to true if searchItem is found; otherwise, false

Type
Promise.<Boolean>

indexOfAF(searchItem, fromIndexopt) → {Promise.<Number>}

resolves to the first index of the specified element or string in an array, string, or array-like object, starting the search at fromIndex; if the value is not found, resolves to -1

Note: when called on an array or array-like object, indexOfAF is run in parallel and all elements will be resolved even if one of the first few indices is a match; if this behavior is not desireable, consider using series.indexOfAF or its alias, io.indexOfAF

Since:
  • 3.5.0
See:
Example
// indexOfAF on an array of promises
const nums = [1, 2, 3].map(n => Promise.resolve(n));

AsyncAF(nums).indexOfAF(2); // Promise that resolves to 1

AsyncAF(nums).indexOfAF(5); // Promise that resolves to -1

AsyncAF(nums).indexOfAF(1, 1); // Promise that resolves to -1

AsyncAF(nums).indexOfAF(3, -1); // Promise that resolves to 2

// indexOfAF on a promise-wrapped string
const string = Promise.resolve('test string');

AsyncAF(string).indexOfAF('test'); // Promise that resolves to 0

AsyncAF(string).indexOfAF('nope'); // Promise that resolves to -1

AsyncAF(string).indexOfAF('test', 5); // Promise that resolves to -1

AsyncAF(string).indexOfAF('string', -6); // Promise that resolves to 5

// indexOfAF on an array-like object
(async function () {
  if (await AsyncAF(arguments).indexOfAF(2) > -1) {
    console.log('2 is included');
  }
})(1, 2, 3); // logs '2 is included'
Parameters:
NameTypeAttributesDefaultDescription
searchItemany

the element or string to search for

fromIndexNumber<optional>
0

the index at which to begin searching for searchItem; a negative value searches from the index of array/string.length - fromIndex; defaults to 0

Returns:

Promise that resolves to the index of searchItem if found; otherwise, -1

Type
Promise.<Number>

joinAF(separatoropt) → {Promise.<String>}

joins all elements of an array or array-like object into a string and resolves to that string

Since:
  • 3.6.0
See:
  • join (alias)
Example
const animals = ['cow', 'chicken', 'cat', 'dog'].map(a => Promise.resolve(a));

// joinAF separator defaults to ','
AsyncAF(animals).joinAF(); // Promise that resolves to 'cow,chicken,cat,dog'

// specifying separator
AsyncAF(animals).joinAF(' & '); // Promise that resolves to 'cow & chicken & cat & dog'

// a non-string separator will be converted to a string
AsyncAF(animals).joinAF(2); // Promise that resolves to 'cow2chicken2cat2dog'

// empty string separator
AsyncAF(animals).joinAF(''); // Promise that resolves to 'cowchickencatdog'

// joining an empty array resolves to an empty string
AsyncAF([]).joinAF('+'); // Promise that resolves to ''

// joinAF on an array-like object
(async function () {
 const list = await AsyncAF(arguments).joinAF(' - ');
 console.log(`Shopping List: ${list}`);
})('eggs', 'milk', 'butter', 'pancake mix');
// Shopping List: eggs - milk - butter - pancake mix
Parameters:
NameTypeAttributesDefaultDescription
separatorany<optional>
','

the string that separates each element in the resulting string; defaults to ','; non-string separators will be converted to strings if necessary; if separator is an empty string '', the array elements are joined without any characters between them

Returns:

Promise that resolves to a string with all array elements joined; if array.length is 0, an empty string '' is returned

Type
Promise.<String>

lastIndexOfAF(searchItem, fromIndexopt) → {Promise.<Number>}

resolves to the last index of the specified element or string, searching backwards in an array, string, or array-like object; fromIndex offsets the start of the search; if the value is not found, resolves to -1

Note: when called on an array or array-like object, lastIndexOfAF is run in parallel and all elements will be resolved even if one of the last few indices is a match; if this behavior is not desireable, consider using series.lastIndexOfAF or its alias, io.lastIndexOfAF

Since:
  • 3.6.0
See:
Example
// lastIndexOfAF on an array of promises
const nums = [1, 1, 2, 2, 3, 3].map(n => Promise.resolve(n));

AsyncAF(nums).lastIndexOfAF(2); // Promise that resolves to 3

AsyncAF(nums).lastIndexOfAF(5); // Promise that resolves to -1

AsyncAF(nums).lastIndexOfAF(2, -4); // Promise that resolves to 2

AsyncAF(nums).lastIndexOfAF(3, -3); // Promise that resolves to -1

// lastIndexOfAF on a promise-wrapped string
const string = Promise.resolve('test string to test');

AsyncAF(string).lastIndexOfAF('test'); // Promise that resolves to 15

AsyncAF(string).lastIndexOfAF('nope'); // Promise that resolves to -1

AsyncAF(string).lastIndexOfAF('test', -5); // Promise that resolves to 0

AsyncAF(string).lastIndexOfAF('to', -7); // Promise that resolves to -1

// lastIndexOfAF on an array-like object
(async function () {
  const lastIndexOf2 = await AsyncAF(arguments).lastIndexOfAF(2);
  console.log(`the last index of 2 in the arguments array-like object is ${lastIndexOf2}`)
})(1, 1, 2, 2, 3, 3); // the last index of 2 in the arguments array-like object is 3
Parameters:
NameTypeAttributesDescription
searchItemany

the element or string to search for

fromIndexNumber<optional>

the index at which to begin searching backwards for searchItem; a negative value searches from the index of array/string.length - fromIndex; defaults to array/string.length - 1

Returns:

Promise that resolves to the last index of searchItem if found; otherwise, -1

Type
Promise.<Number>

mapAF(callback, thisArgopt) → {Promise.<Array>}

creates a new Array with the results of calling a provided function on every element in the original array

if any elements are a Promise, they will first be resolved in parallel and then processed

Note: if you'd rather resolve and process elements in series, consider using series.mapAF or its alias, io.mapAF

Since:
  • 3.0.0
See:
Example
const promises = [1, 2].map(n => Promise.resolve(n));


// basic usage
const doubled = AsyncAF(promises).mapAF(el => el * 2);

console.log(doubled); // Promise that resolves to [2, 4]

AsyncAF.logAF(doubled); // logs [2, 4]


// using .then
AsyncAF(promises).mapAF(el => el * 3).then(tripled => {
  console.log(tripled); // logs [3, 6]
});


// inside an async function
(async () => {
  const quadrupled = await AsyncAF(promises).mapAF(
    el => el * 4
  );
  console.log(quadrupled); // logs [4, 8]
})();
Parameters:
NameTypeAttributesDescription
callbackFunction

function that produces an element of the new Array

callback accepts three arguments:

  • currentValue value of the current element being processed in the array
  • index(optional) index of currentValue in the array
  • array(optional) the array that mapAF is being applied to
thisArgObject<optional>

value to use as this when executing callback

Returns:

Promise that resolves to a new Array with each element being the result of calling callback on each original element

Type
Promise.<Array>

reduceAF(callback, initialValueopt) → {Promise.<any>}

applies a function against an accumulator and each element in an array (from left to right) to reduce it to a single value

if any elements are a Promise, they will first be resolved in parallel and then processed in series

Note: if this behavior is not desirable, consider using series.reduceAF or its alias, io.reduceAF; that way, if any elements are a Promise, they will both be resolved in series and processed in series

Since:
  • 3.1.0
See:
Example
const promises = [1, 2, 3].map(n => Promise.resolve(n));


// basic usage
const sum = AsyncAF(promises).reduceAF((sum, num) => sum + num);

console.log(sum); // Promise that resolves to 6

AsyncAF.logAF(sum); // logs 6


// using .then
AsyncAF(promises).reduceAF((sum, num) => sum + num).then(sum => {
  console.log(sum); // logs 6
});


// inside an async function
(async () => {
  const sum = await AsyncAF(promises).reduceAF((sum, num) => sum + num);
  console.log(sum); // logs 6
})();


// using an initial value
AsyncAF(promises).reduceAF((sum, num) => sum + num, 12) // Promise that resolves to 18
Parameters:
NameTypeAttributesDescription
callbackFunction

function to execute for each element

callback accepts up to four arguments:

  • accumulator accumulates the callback's return values; the accumulated value previously returned in the last invocation of the callback, or initialValue, if supplied
  • currentValue value of the current element being processed in the array
  • index(optional) index of currentValue in the array
  • array(optional) the array that reduceAF is being applied to
initialValueany<optional>

value to use as the first argument to the first call of the callback; if no initial value is supplied, the first element in the array will be used; note: calling reduceAF on an empty array with no initial value will throw an error

Returns:

Promise that resolves to the reduced value

Type
Promise.<any>

someAF(callback, thisArgopt) → {Promise.<Boolean>}

tests whether at least one element in the array passes the test implemented by the provided callback function

if any elements are a Promise, they will first be resolved in parallel and then tested

Note: since someAF is run in parallel, callback will be run on all elements even if one of the first few elements passes the test; if this behavior is not desireable, consider using series.someAF or its alias, io.someAF

Since:
  • 3.3.0
See:
Example
const promises = [1, 2, 3].map(n => Promise.resolve(n));


// basic usage
const someAreEven = AsyncAF(promises).someAF(n => n % 2 === 0);

console.log(someAreEven); // Promise that resolves to true

AsyncAF.logAF(someAreEven); // logs true


// using .then
AsyncAF(promises).someAF(n => n % 2 === 0).then(someAreEven => {
  console.log(someAreEven); // logs true
});


// inside an async function
(async () => {
  const someAreStrings = await AsyncAF(promises).someAF(
    n => typeof n === 'string'
  );
  console.log(someAreStrings); // logs false
})();
Parameters:
NameTypeAttributesDescription
callbackFunction

function that tests each element of the array

callback accepts three arguments:

  • currentValue value of the current element being processed in the array
  • index(optional) index of currentValue in the array
  • array(optional) the array that someAF is being applied to
thisArgObject<optional>

value to use as this when executing callback

Returns:

Promise that resolves to true if the callback function returns a truthy value for any array element; otherwise, false

Type
Promise.<Boolean>

(static) logAF(…items) → {Promise.<undefined>}

logs items to the console in the order given

if any items are a promise, they will first be resolved in parallel and then logged

import { logAF } from 'async-af';

const promise = new Promise(resolve => setTimeout(
 () => resolve(2), 1000)
);

logAF(1, promise, 3);

// @filename.js:6:12:
// 1 2 3
// in 0.998 secs

Note: since logAF returns a promise, the items in the previous example would be logged after any synchronous calls to console.log

to produce in-order logging with any surrounding calls to console.log, await logAF:

logAF.options({ label: false, duration: false });

(async () => {
  console.log(1);
  // ...some code
  await logAF(promise);
  // ...some more code
  console.log(3);
})();

// 1
// 2
// 3

experimental feature: the label may not work correctly in all environments; to turn the label off, set label to false in logAF.options, where you can also change the label's format

Since:
  • 3.0.0
See:
  • log (alias)
  • logAF.options to turn the label off or change its format
  • logAF.options.reset to reset options to default
Parameters:
NameTypeAttributesDescription
itemsany<repeatable>

The items to print (log to the console)

Returns:

returns a Promise that logs items to the console

Type
Promise.<undefined>

(static) logAF.options(options: {labelopt, durationopt, labelFormatopt}) → {undefined}

Sets logging options for logAF

accepts an options object with the following optional properties:

  • label (Boolean) - set to false to disable logging the location of calls to logAF
  • duration (Boolean) - set to false to disable logging the time it takes (in secs) to complete each call to logAF
  • labelFormat (String|Function) - alters the format of logAF labels; choose between file (default), path, parent, arrow, or a custom string or function
const promise = new Promise(resolve => setTimeout(
  () => resolve(1), 1000)
);

default logging

logAF(promise, 2);

// @filename.js:24:1:
// 1 2
// in 0.998 secs

turn off label

logAF.options({ label: false });
logAF(promise, 2);

// 1 2
// in 0.999 secs

turn off duration

logAF.options({ duration: false });
logAF(promise, 2);

// @filename.js:24:1:
// 1 2

change labelFormat

● file (default)

logAF.options({ labelFormat: 'file' });
logAF(promise, 2);

// @filename.js:24:1:
// 1 2
// in 0.998 secs

● path

logAF.options({ labelFormat: 'path' });
logAF(promise, 2);

// @/Path/to/current/directory/filename.js:24:1:
// 1 2
// in 0.997 secs

● parent

logAF.options({ labelFormat: 'parent' });
logAF(promise, 2);

// @parentDirectory/filename.js:24:1:
// 1 2
// in 0.998 secs

● arrow

logAF.options({ labelFormat: 'arrow' });
logAF(promise, 2);

// ========================> 1 2
// in 0.999 secs

● custom (create your own labelFormat)

  • to set a custom labelFormat, set it to any string other than the formats above
logAF.options({
 labelFormat: 'I logged this:'
});
logAF(promise, 2);

// I logged this: 1 2
// in 1.000 secs
  • labelFormat also accepts a function with access to an object containing the location variables file, path, parent, arrow, line, and col

e.g., to set the labelFormat to file:line:col =>:

logAF.options({
 labelFormat: ({file, line, col}) => `${file}:${line}:${col} =>`
});
logAF(promise, 2);

// filename.js:24:1 => 1 2
// in 0.998 secs

and just to demonstrate all the location variables in one custom format:

logAF.options({
  labelFormat: ({arrow, line, col, parent, file, path}) =>
`${arrow}
line: ${line}
col: ${col}
parent: ${parent}
file: ${file}
path: ${path}
`
});
logAF(promise, 2);

// ========================>
// line: 24
// col: 1
// parent: parentDirectory/
// file: filename.js
// path: /Full/path/to/the/parentDirectory/
// 1 2
// in 0.998 secs

to reset logAF.options to its default values, call logAF.options.reset

logAF.options.reset();

// options are now:
// label: true,
// duration: true,
// labelFormat: 'file'
See:
  • logAF
  • logAF.options.reset to reset options to default
Parameters:
NameTypeDescription
optionsObject

the options for logAF

Properties
NameTypeAttributesDefaultDescription
labelBoolean<optional>
true

set to false to turn off the label

durationBoolean<optional>
true

set to false to turn off duration

labelFormatString | Function<optional>
file

see examples for sample label formats

Returns:

sets the options for logAF

Type
undefined

series → {AsyncAF.<any>}

indicates that the next method invoked should be performed in series

when you need to perform a method in series rather than in parallel, prepend the method with series; e.g.:

AsyncAF(promises).series.forEachAF(callback)

series can currently be chained with:

Since:
  • 7.0.0
See:
  • io (alias)
Example
import delay from 'delay'; // https://www.npmjs.com/package/delay

const nums = [2, 1];

// perform a serial forEach by chaining series and forEachAF
(async () => {
  const start = Date.now();

  await AsyncAF(nums).series.forEachAF(async num => {
    await delay(num * 1000);
    console.log(num, `at ~${Date.now() - start} ms`);
  });

  console.log(`total: ~${Date.now() - start} ms`);
})();

// logs:
// 2  'at ~2000 ms'
// 1  'at ~3000 ms'
// total: ~3000 ms


// perform a parallel forEach by omitting series
(async () => {
  const start = Date.now();

  await AsyncAF(nums).forEachAF(async num => {
    await delay(num * 1000);
    console.log(num, `at ~${Date.now() - start} ms`);
  });

  console.log(`total: ~${Date.now() - start} ms`);
})();

// logs:
// 1  'at ~1000 ms'
// 2  'at ~2000 ms'
// total: ~2000 ms
Returns:

returns an instance of AsyncAF that will perform the next method invocation serially

Type
AsyncAF.<any>

io → {AsyncAF.<any>}

io (in order) indicates that the next method invoked should be performed in series

when you need to perform a method in series rather than in parallel, prepend the method with io; e.g.:

AsyncAF(promises).io.forEachAF(callback)

io is an alias for series; see series's documentation for more

Since:
  • 7.0.0
See:
Returns:

returns an instance of AsyncAF that will perform the next method invocation serially

Type
AsyncAF.<any>

splitAF(separatoropt, limitopt) → {Promise.<Array.<String>>}

splits a string into an array of substrings, using a specified separator to determine where to make each split

Since:
  • 5.1.0
See:
  • split (alias)
Example
// basic usage
const str = Promise.resolve('s; p; l; i; t');

AsyncAF(str).splitAF('; '); // Promise that resolves to ['s', 'p', 'l', 'i', 't']

// no separator specified or separator not found
const str = Promise.resolve('splat');

AsyncAF(str).splitAF(); // Promise that resolves to ['splat']
AsyncAF(str).splitAF('n/a'); // Promise that resolves to ['splat']

// split to individual characters
const str = Promise.resolve('splitAF');

AsyncAF(str).splitAF(''); // Promise that resolves to ['s', 'p', 'l', 'i', 't', 'A', 'F']

// split on a regular expression
const str = Promise.resolve('splittedAF');

AsyncAF(str).splitAF(/sp|ted/); // Promise that resolves to ['', 'lit', 'AF']

// and w/ capturing parentheses

AsyncAF(str).splitAF(/(lit|AF)/); // Promise that resolves to ['sp', 'lit', '', 'AF', '']

// setting limit
const str = Promise.resolve('splitted');

AsyncAF(str).splitAF('', 5); // Promise that resolves to ['s', 'p', 'l', 'i', 't']
AsyncAF(str).splitAF('', 12); // Promise that resolves to ['s', 'p', 'l', 'i', 't', 't', 'e', 'd']
AsyncAF(str).splitAF('', 0); // Promise that resolves to []
Parameters:
NameTypeAttributesDescription
separatorString | RegExp<optional>

a string or regular expression that denotes the points at which each split should occur

  • if a plain-text separator contains more than one character, the entire separator must be found to represent a split point
  • if separator is omitted or does not occur in the string, the array returned will contain one element consisting of the entire string
  • if separator is an empty string, the string is converted to an array of individual characters
  • if separator is a regular expression, the array returned will also contain any separators found as a result of matches within capturing parentheses
limitNumber<optional>

integer specifying a limit on the number of elements to be included in the result; when provided, the string is split at each occurrence of the specified separator but stops including elements when the limit is reached (or the end of the string, if reached sooner); any left-over text is not included in the result

Returns:

Promise that resolves to an array of strings, split at each point the separator occurs in the given string

Type
Promise.<Array.<String>>