then-utils
A collection of useful, promise-returing utilities. No dependencies (other then Node builtins).
Utilities
All utilities return Promise
s.
callWithPromiseOrCallback
Arguments
func
- the function to call...args
- extra arguments to pass to the function
Description
Calls the given func
with optional extra ...args
, and a Node-style callback (error, data). If the function returns a Promise
, then it attaches it's resolve
and reject
to that Promise
. If the callback is called, then it will reject
if error
is given, or resolve
with the given data
.
Usage
const callWithPromiseOrCallback = ;const addWithCallback addWithPromise = ; ; ;
returnPromiseOrCallback
Arguments
callbackArg
- the callback argumenthandler
- thePromise
handler
Description
This function is mainly for API developers who still need to support callbacks but want to migrate to Promise
s. The callbackArg
should be whatever your callback argument is in your function. If the callbackArg
is a function, then it is called as a Node-style callback (error, data) when you resolve
or reject
in the handler
. Otherwise, it returns a Promise
with the given handler
.
Usage
const returnPromiseOrCallback = ; { return ;} ; ;
asyncFor
Arguments
arrOrObjOrNum
- an array, object, or number to loop ononloop
- a function called for every iteration of the loop
Description
TL;DR, just see the usage below
Asynchronously loops using setImmediate
. If arrOrObjOrNum
is a number, it'll be converted into an array by pushing null
into an array arrOrObjOrNum
times, and looping on that. It uses Object.keys()
to get the keys to loop through, except that if the key can be parsed to a Number
(with parseInt()
) then it'll convert it to a Number
. It'll call onloop
for every iteration of the loop with the current key (or index, for an array), the item at that position (null
if arrOrObjOrNum
is a number), and a breakLoop function (to exit the loop early). If onloop
returns a Promise
, it'll wait until the Promise
is resolve
d to goto the next loop. If the Promise
is reject
ed, it'll stop looping and reject it's own Promise
. Otherwise, it'll add a Node-style callback (error, data) argument to onloop
's arguments, where if given an error, it'll act as if it were reject
ed and end the loop.
Usage
const asyncFor = ; const myArray = 9 3 4;const myObject = nom: 'hi' foo: 8; ; ; // then, catch handlers here... ; // then, catch handlers here... ; // then, catch handlers here... ;
rmrf
Arguments
pathname
- the path to remove recursively (can be a folder or a file)
Description
Remove a file or folder from the specified pathname
. If the path is a folder, it is removed recursively. If the file or folder isn't found, then it will resolve
, since the goal (having the path not exist) is already achieved.
Usage
const rmrf = ; ;
mkdirp
Arguments
pathname
- the full path for the new folder
Description
Creates a folder at the specified path recursively, that is, it will create any folders that don't exist in the path.
Usage
const mkdirp = ; ;
filterByExtension
Arguments
pathname
- the path search in to filter it's filesext
- the extension to filter for
Description
Reads the files of the pathname
, and filters them. If their extension matches the specified ext
, then they will be returned.
Usage
const filterByExtension = ; ;
writeFile
Arguments
See fs.writeFile
.
Description
Promise
-ified fs.writeFile
.
Usage
const writeFile = ; ;
exec
Arguments
See child_process.exec
.
Description
Promise
-ified child_process.exec
.
Usage
const exec = ; ;
mv
Arguments
See fs.rename
.
Description
Promise
-ified fs.rename
.
Usage
const mv = ; ;
cpr
Arguments
frompath
- the path to copy fromtopath
- the path to copy to
Description
Copies a folder or file recursively from frompath
to topath
.
Usage
const cpr = ; ;
parseArgs
Arguments
args
- the arguments to parse
Description
Parses the given arguments into an object. I'm not going to spend time explaining this, just take the example.
Usage
const parseArgs = ; ;
spawn
Arguments
See child_process.spawn
.
Description
Promise
-ified child_process.spawn
.
Usage
const spawn = ; const child = ; childcmd; // contains the spawned ChildProcess child;
sleep
Arguments
ms
- number of milliseconds to wait
Description
A "sleep" function (a.k.a. setTimeout
), resolve
s after the given time is up.
Usage
const sleep = ; ;
readFile
Arguments
See fs.readFile
.
Description
Promise
-ified fs.readFile
.
Usage
const readFile = ; ;