mentie 0.2.29 → 0.2.31

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.
@@ -172,8 +172,7 @@ log.error = function( ...messages ) {
172
172
 
173
173
  // Check if the loglevel matches this call
174
174
  const levels = [ 'error', 'warn', 'info' ]
175
- const should_log = dev || levels.includes( env.loglevel() )
176
- if( !dev || !should_log ) return
175
+ if( dev || !should_log( levels ) ) return
177
176
 
178
177
  // Log the messages if the loglevel matches
179
178
  console.error( '🚨 ', ...messages )
@@ -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.29",
3
+ "version": "0.2.31",
4
4
  "description": "Mentor's toolbelt",
5
5
  "type": "module",
6
6
  "main": "index.js",