Utils Class
Collection of various utility functions.
Copyright (c) 2014 ITSA - https://github.com/itsa New BSD License - http://choosealicense.com/licenses/bsd-3-clause/
Item Index
Methods
- _asynchronizer static
- async
- async
- idGenerator
- later
Methods
_asynchronizer
-
callbackFn
Forces a function to be run asynchronously, but as fast as possible. In Node.js
this is achieved using setImmediate
or process.nextTick
.
Parameters:
-
callbackFn
FunctionThe function to call asynchronously
async
-
callbackFn
-
[invokeAfterFn=true]
Invokes the callbackFn once in the next turn of the JavaScript event loop. If the function requires a specific execution context or arguments, wrap it with Function.bind.
I.async returns an object with a cancel method. If the cancel method is called before the callback function, the callback function won't be called.
Parameters:
Returns:
An object with a cancel method. If the cancel method is called before the callback function, the callback function won't be called.
async
-
callbackFn
-
[invokeAfterFn=true]
Invokes the callbackFn once in the next turn of the JavaScript event loop. If the function requires a specific execution context or arguments, wrap it with Function.bind.
I.async returns an object with a cancel method. If the cancel method is called before the callback function, the callback function won't be called.
Parameters:
Returns:
An object with a cancel method. If the cancel method is called before the callback function, the callback function won't be called.
idGenerator
-
[namespace]
-
[start]
Generates an unique id with the signature: "namespace-follownr"
Parameters:
-
[namespace]
String optionalnamespace to prepend the generated id. When ignored, the generator just returns a number.
-
[start]
Number optionalstartvalue for the next generated id. Any further generated id's will preceed this id. If
start
is lower or equal than the last generated id, it will be ignored.
Returns:
Example:
var generator = require('core-utils-idgenerator');
console.log(generator()); // --> 1
console.log(generator()); // --> 2
console.log(generator(1000)); // --> 1000
console.log(generator()); // --> 1001
console.log(generator('Parcel, 500')); // -->"Parcel-500"
console.log(generator('Parcel')); // -->"Parcel-501"
later
-
callbackFn
-
[timeout]
-
[periodic]
Invokes the callbackFn after a timeout (asynchronous). If the function requires a specific execution context or arguments, wrap it with Function.bind.
To invoke the callback function periodic, set 'periodic' either 'true', or specify a second timeout. If number, then periodic is considered 'true' but with a perdiod defined by 'periodic', which means: the first timer executes after 'timeout' and next timers after 'period'.
I.later returns an object with a cancel method. If the cancel() method is called before the callback function, the callback function won't be called.
Parameters:
-
callbackFn
Functionthe function to execute.
-
[timeout]
Number optionalthe number of milliseconds to wait until the callbackFn is executed. when not set, the callback function is invoked once in the next turn of the JavaScript event loop.
-
[periodic]
Boolean | Number optionalif true, executes continuously at supplied, if number, then periodic is considered 'true' but with a perdiod defined by 'periodic', which means: the first timer executes after 'timeout' and next timers after 'period'. The interval executes until canceled.
Returns:
a timer object. Call the cancel() method on this object to stop the timer.