mentie 0.2.28 → 0.2.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/modules/cache.js CHANGED
@@ -36,14 +36,15 @@ export function cache( key, value ) {
36
36
  * Function to inspect concurrency.
37
37
  *
38
38
  * @param {Function} logger - The logger function.
39
+ * @param {string} key_prefix - The key for the concurrency value, used to tag the logging
39
40
  * @returns {number} - The concurrency value.
40
41
  */
41
- export function concurrency( logger ) {
42
+ export function concurrency( logger, key_prefix ) {
42
43
 
43
44
  // Get the concurrency key
44
45
  let key = cache( `concurrency_key` )
45
46
  if( !key ) {
46
- cache( `concurrency_key`, Date.now() )
47
+ cache( `concurrency_key`, `${ key_prefix }_${ Date.now() }` )
47
48
  key = cache( `concurrency_key` )
48
49
  }
49
50
 
@@ -100,4 +100,29 @@ export async function throttle_and_retry( async_function_array = [], { max_paral
100
100
  logger( `Starting queue of ${ async_function_array.length } functions with options: `, throttle_config )
101
101
  return Throttle.all( retryable_async_functions, throttle_config )
102
102
 
103
+ }
104
+
105
+ /**
106
+ * A function that adds a timeout to a promise.
107
+ *
108
+ * @param {Promise} promise - The promise to add a timeout to.
109
+ * @param {number} [timeout_in_ms=60000] - The timeout duration in milliseconds. Default is 60000ms (1 minute).
110
+ * @param {boolean} [throw_on_timeout=true] - Whether to throw an error on timeout. Default is true.
111
+ * @returns {Promise} - A promise that resolves with the result of the original promise or a timeout message.
112
+ */
113
+ export async function promise_timeout( promise, timeout_in_ms=60_000, throw_on_timeout=true ) {
114
+
115
+ // Timeout finction
116
+ const timeout = () => new Promise( ( res, rej ) => setTimeout( throw_on_timeout ? rej : () => res( 'timed out' ), timeout_in_ms ) )
117
+
118
+ // Race the promise against the timeout
119
+ return Promise.race( [
120
+
121
+ // If the promise resolves first, return the result (including throwsing on error)
122
+ promise,
123
+
124
+ // If this resolves first, this function throws or returns a timeout message
125
+ timeout()
126
+ ] )
127
+
103
128
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mentie",
3
- "version": "0.2.28",
3
+ "version": "0.2.30",
4
4
  "description": "Mentor's toolbelt",
5
5
  "type": "module",
6
6
  "main": "index.js",