You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/sdk/io/overview.mdx
+4Lines changed: 4 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,6 +40,10 @@ If you want to send an event from outside a run (e.g. just from your backend) yo
40
40
41
41
`io.backgroundFetch()` allows you to fetch data from a URL that can take longer that the serverless timeout. The actual `fetch` request is performed on the Trigger.dev platform, and the response is sent back to you. An example use case is fetching data from a slow API, like some AI endpoints.
42
42
43
+
### [random()](/sdk/io/random)
44
+
45
+
`io.random()` is identical to `Math.random()` when called without options but ensures your random numbers are not regenerated on resume or retry. It will return a pseudo-random floating-point number between optional `min` (default: 0, inclusive) and `max` (default: 1, exclusive). Can optionally `round` to the nearest integer.
46
+
43
47
### [try()](/sdk/io/try)
44
48
45
49
`io.try()` allows you to run Tasks and catch any errors that are thrown, it's similar to a normal `try/catch` block but works with [io.runTask()](/sdk/io/runtask).
description: "`io.random()` is identical to `Math.random()` when called without options but ensures your random numbers are not regenerated on resume or retry. It will return a pseudo-random floating-point number between optional `min` (default: 0, inclusive) and `max` (default: 1, exclusive). Can optionally `round` to the nearest integer."
Controls rounding to the nearest integer. Any `max` integer will become inclusive when enabled. Rounding with floating-point bounds may cause unexpected skew and boundary inclusivity.
18
+
</ResponseField>
19
+
20
+
## Returns
21
+
22
+
A `Promise` that resolves with a pseudo-random number. Always resolves to an integer when rounding is enabled.
Copy file name to clipboardExpand all lines: packages/trigger-sdk/src/io.ts
+85-1Lines changed: 85 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -212,6 +212,90 @@ export class IO {
212
212
});
213
213
}
214
214
215
+
/** `io.random()` is identical to `Math.random()` when called without options but ensures your random numbers are not regenerated on resume or retry. It will return a pseudo-random floating-point number between optional `min` (default: 0, inclusive) and `max` (default: 1, exclusive). Can optionally `round` to the nearest integer.
216
+
* @param cacheKey Should be a stable and unique key inside the `run()`. See [resumability](https://trigger.dev/docs/documentation/concepts/resumability) for more information.
217
+
* @param min Sets the lower bound (inclusive). Can't be higher than `max`.
218
+
* @param max Sets the upper bound (exclusive). Can't be lower than `min`.
219
+
* @param round Controls rounding to the nearest integer. Any `max` integer will become inclusive when enabled. Rounding with floating-point bounds may cause unexpected skew and boundary inclusivity.
220
+
*/
221
+
asyncrandom(
222
+
cacheKey: string|any[],
223
+
{
224
+
min =0,
225
+
max =1,
226
+
round =false,
227
+
}: {
228
+
min?: number;
229
+
max?: number;
230
+
round?: boolean;
231
+
}={}
232
+
){
233
+
returnawaitthis.runTask(
234
+
cacheKey,
235
+
async(task)=>{
236
+
if(min>max){
237
+
thrownewError(
238
+
`Lower bound can't be higher than upper bound - min: ${min}, max: ${max}`
239
+
);
240
+
}
241
+
242
+
if(min===max){
243
+
awaitthis.logger.warn(
244
+
`Lower and upper bounds are identical. The return value is not random and will always be: ${min}`
"Rounding enabled with floating-point bounds. This may cause unexpected skew and boundary inclusivity."
257
+
);
258
+
}
259
+
260
+
constrounded=Math.round(withinBounds);
261
+
262
+
returnrounded;
263
+
},
264
+
{
265
+
name: "random",
266
+
icon: "dice-5-filled",
267
+
params: { min, max, round },
268
+
properties: [
269
+
...(min===0
270
+
? []
271
+
: [
272
+
{
273
+
label: "min",
274
+
text: String(min),
275
+
},
276
+
]),
277
+
...(max===1
278
+
? []
279
+
: [
280
+
{
281
+
label: "max",
282
+
text: String(max),
283
+
},
284
+
]),
285
+
...(round===false
286
+
? []
287
+
: [
288
+
{
289
+
label: "round",
290
+
text: String(round),
291
+
},
292
+
]),
293
+
],
294
+
style: {style: "minimal"},
295
+
}
296
+
);
297
+
}
298
+
215
299
/** `io.wait()` waits for the specified amount of time before continuing the Job. Delays work even if you're on a serverless platform with timeouts, or if your server goes down. They utilize [resumability](https://trigger.dev/docs/documentation/concepts/resumability) to ensure that the Run can be resumed after the delay.
216
300
* @param cacheKey Should be a stable and unique key inside the `run()`. See [resumability](https://trigger.dev/docs/documentation/concepts/resumability) for more information.
217
301
* @param seconds The number of seconds to wait. This can be very long, serverless timeouts are not an issue.
@@ -877,7 +961,7 @@ export class IO {
877
961
*/
878
962
brb=this.yield.bind(this);
879
963
880
-
/** `io.try()` allows you to run Tasks and catch any errors that are thrown, it's similar to a normal `try/catch` block but works with [io.runTask()](/sdk/io/runtask).
964
+
/** `io.try()` allows you to run Tasks and catch any errors that are thrown, it's similar to a normal `try/catch` block but works with [io.runTask()](https://trigger.dev/docs/sdk/io/runtask).
881
965
* A regular `try/catch` block on its own won't work as expected with Tasks. Internally `runTask()` throws some special errors to control flow execution. This is necessary to deal with resumability, serverless timeouts, and retrying Tasks.
882
966
* @param tryCallback The code you wish to run
883
967
* @param catchCallback Thhis will be called if the Task fails. The callback receives the error
0 commit comments