@radomiej/compiler-types 3.0.9-types.0

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.
@@ -0,0 +1,647 @@
1
+ /// <reference no-default-lib="true"/>
2
+ /// <reference types="@rbxts/types"/>
3
+
4
+ // Based on roblox-lua-promise v3.1.0
5
+ // https://eryn.io/roblox-lua-promise/
6
+
7
+ declare namespace Promise {
8
+ namespace Error {
9
+ type Kind = "ExecutionError" | "AlreadyCancelled" | "NotResolvedInTime" | "TimedOut";
10
+ }
11
+
12
+ interface ErrorOptions {
13
+ trace: string;
14
+ context: string;
15
+ kind: Promise.Error.Kind;
16
+ }
17
+
18
+ interface Error {
19
+ readonly error: string;
20
+ readonly trace?: string;
21
+ readonly context?: string;
22
+ readonly kind?: Promise.Error.Kind;
23
+ readonly parent?: Promise.Error;
24
+ readonly createdTick: number;
25
+ readonly createdTrace: string;
26
+
27
+ extend(options?: Partial<Promise.ErrorOptions>): Promise.Error;
28
+
29
+ getErrorChain(): Array<Promise.Error>;
30
+ }
31
+
32
+ interface ErrorConstructor {
33
+ readonly Kind: { readonly [K in Promise.Error.Kind]: K };
34
+
35
+ is: (value: unknown) => value is Promise.Error;
36
+
37
+ isKind: (value: unknown, kind: Promise.Error.Kind) => value is Promise.Error;
38
+
39
+ new (options?: Partial<Promise.ErrorOptions>, parent?: Promise.Error): Promise.Error;
40
+ }
41
+ }
42
+
43
+ interface PromiseLike<T> {
44
+ /**
45
+ * Chains onto an existing Promise and returns a new Promise.
46
+ * > Within the failure handler, you should never assume that the rejection value is a string. Some rejections within the Promise library are represented by Error objects. If you want to treat it as a string for debugging, you should call `tostring` on it first.
47
+ *
48
+ * Return a Promise from the success or failure handler and it will be chained onto.
49
+ */
50
+ then<TResult1 = T, TResult2 = never>(
51
+ this: Promise<T>,
52
+ onResolved?: ((value: T) => TResult1 | Promise<TResult1>) | void,
53
+ onRejected?: ((reason: any) => TResult2 | Promise<TResult2>) | void,
54
+ ): Promise<TResult1 | TResult2>;
55
+ }
56
+
57
+ /**
58
+ * Represents the completion of an asynchronous operation
59
+ */
60
+ interface Promise<T> {
61
+ /**
62
+ * Chains onto an existing Promise and returns a new Promise.
63
+ * > Within the failure handler, you should never assume that the rejection value is a string. Some rejections within the Promise library are represented by Error objects. If you want to treat it as a string for debugging, you should call `tostring` on it first.
64
+ *
65
+ * Return a Promise from the success or failure handler and it will be chained onto.
66
+ */
67
+ then<TResult1 = T, TResult2 = never>(
68
+ this: Promise<T>,
69
+ onResolved?: ((value: T) => TResult1 | Promise<TResult1>) | void,
70
+ onRejected?: ((reason: any) => TResult2 | Promise<TResult2>) | void,
71
+ ): Promise<TResult1 | TResult2>;
72
+
73
+ /**
74
+ * Chains onto an existing Promise and returns a new Promise.
75
+ * > Within the failure handler, you should never assume that the rejection value is a string. Some rejections within the Promise library are represented by Error objects. If you want to treat it as a string for debugging, you should call `tostring` on it first.
76
+ *
77
+ * Return a Promise from the success or failure handler and it will be chained onto.
78
+ */
79
+ andThen<TResult1 = T, TResult2 = never>(
80
+ this: Promise<T>,
81
+ onResolved?: ((value: T) => TResult1 | Promise<TResult1>) | void,
82
+ onRejected?: ((reason: any) => TResult2 | Promise<TResult2>) | void,
83
+ ): Promise<TResult1 | TResult2>;
84
+
85
+ /**
86
+ * Shorthand for `Promise:andThen(nil, failureHandler)`.
87
+ *
88
+ * Returns a Promise that resolves if the `failureHandler` worked without encountering an additional error.
89
+ */
90
+ catch<TResult = never>(
91
+ this: Promise<T>,
92
+ onRejected?: ((reason: any) => TResult | Promise<TResult>) | void,
93
+ ): Promise<T | TResult>;
94
+
95
+ /**
96
+ * Similar to [Promise.andThen](https://eryn.io/roblox-lua-promise/lib/#andthen), except the return value is the same as the value passed to the handler. In other words, you can insert a `:tap` into a Promise chain without affecting the value that downstream Promises receive.
97
+ * ```lua
98
+ * getTheValue()
99
+ * :tap(print)
100
+ * :andThen(function(theValue))
101
+ * print("Got", theValue, "even though print returns nil!")
102
+ * end)
103
+ * ```
104
+ * If you return a Promise from the tap handler callback, its value will be discarded but `tap` will still wait until it resolves before passing the original value through.
105
+ */
106
+ tap(this: Promise<T>, tapHandler: (value: T) => void): Promise<T>;
107
+
108
+ /**
109
+ * Set a handler that will be called regardless of the promise's fate. The handler is called when the promise is resolved, rejected, _or_ cancelled.
110
+ *
111
+ * Returns a new promise chained from this promise.
112
+ *
113
+ * > Set a handler that will be called regardless of the promise's fate. The handler is called when the promise is
114
+ resolved, rejected, *or* cancelled.
115
+ Returns a new Promise that:
116
+ - resolves with the same values that this Promise resolves with.
117
+ - rejects with the same values that this Promise rejects with.
118
+ - is cancelled if this Promise is cancelled.
119
+ If the value you return from the handler is a Promise:
120
+ - We wait for the Promise to resolve, but we ultimately discard the resolved value.
121
+ - If the returned Promise rejects, the Promise returned from `finally` will reject with the rejected value from the
122
+ *returned* promise.
123
+ - If the `finally` Promise is cancelled, and you returned a Promise from the handler, we cancel that Promise too.
124
+ Otherwise, the return value from the `finally` handler is entirely discarded.
125
+ :::note Cancellation
126
+ As of Promise v4, `Promise:finally` does not count as a consumer of the parent Promise for cancellation purposes.
127
+ This means that if all of a Promise's consumers are cancelled and the only remaining callbacks are finally handlers,
128
+ the Promise is cancelled and the finally callbacks run then and there.
129
+ Cancellation still propagates through the `finally` Promise though: if you cancel the `finally` Promise, it can cancel
130
+ its parent Promise if it had no other consumers. Likewise, if the parent Promise is cancelled, the `finally` Promise
131
+ will also be cancelled.
132
+ * ```lua
133
+ * local thing = createSomething()
134
+ *
135
+ * doSomethingWith(thing)
136
+ * :andThen(function()
137
+ * print("It worked!")
138
+ * -- do something..
139
+ * end)
140
+ * :catch(function()
141
+ * warn("Oh no it failed!")
142
+ * end)
143
+ * :finally(function()
144
+ * -- either way, destroy thing
145
+ *
146
+ * thing:Destroy()
147
+ * end)
148
+ * ```
149
+ */
150
+ finally<TResult = never>(
151
+ this: Promise<T>,
152
+ onSettled?: (() => TResult | Promise<TResult>) | void,
153
+ ): Promise<T | TResult>;
154
+
155
+ /**
156
+ * Attaches an `andThen` handler to this Promise that calls the given callback with the predefined arguments. The resolved value is discarded.
157
+ * ```lua
158
+ * promise:andThenCall(someFunction, "some", "arguments")
159
+ * ```
160
+ * This is sugar for
161
+ * ```lua
162
+ * promise:andThen(function()
163
+ * return someFunction("some", "arguments")
164
+ * end)
165
+ * ```
166
+ */
167
+ andThenCall<P extends Array<any>, R>(this: Promise<T>, callback: (...args: P) => R, ...args: P): Promise<R>;
168
+
169
+ /**
170
+ * Same as `andThenCall`, except for `finally`.
171
+ *
172
+ * Attaches a `finally` handler to this Promise that calls the given callback with the predefined arguments.
173
+ */
174
+ finallyCall<P extends Array<any>, R>(this: Promise<T>, callback: (...args: P) => R, ...args: P): Promise<R>;
175
+
176
+ /**
177
+ * Attaches an `andThen` handler to this Promise that discards the resolved value and returns the given value from it.
178
+ * ```lua
179
+ * promise:andThenReturn("value")
180
+ * ```
181
+ * This is sugar for
182
+ * ```lua
183
+ * promise:andThen(function()
184
+ * return "value"
185
+ * end)
186
+ * ```
187
+ * > Promises are eager, so if you pass a Promise to `andThenReturn`, it will begin executing before `andThenReturn` is reached in the chain. Likewise, if you pass a Promise created from [Promise.reject](https://eryn.io/roblox-lua-promise/lib/#reject) into `andThenReturn`, it's possible that this will trigger the unhandled rejection warning. If you need to return a Promise, it's usually best practice to use [Promise.andThen](https://eryn.io/roblox-lua-promise/lib/#andthen).
188
+ */
189
+ andThenReturn<U>(this: Promise<T>, value: U): Promise<U>;
190
+
191
+ /**
192
+ * Attaches a `finally` handler to this Promise that discards the resolved value and returns the given value from it.
193
+ * ```lua
194
+ * promise:finallyReturn("value")
195
+ * ```
196
+ * This is sugar for
197
+ * ```lua
198
+ * promise:finally(function()
199
+ * return "value"
200
+ * end)
201
+ * ```
202
+ */
203
+ finallyReturn<U>(this: Promise<T>, value: U): Promise<U>;
204
+
205
+ /**
206
+ * Returns a new Promise that resolves if the chained Promise resolves within `seconds` seconds, or rejects if execution time exceeds `seconds`. The chained Promise will be cancelled if the timeout is reached.
207
+ *
208
+ * Rejects with `rejectionValue` if it is non-nil. If a `rejectionValue` is not given, it will reject with a `Promise.Error(Promise.Error.Kind.TimedOut)`. This can be checked with `Error.isKind`.
209
+ * ```lua
210
+ * getSomething():timeout(5):andThen(function(something)
211
+ * -- got something and it only took at max 5 seconds
212
+ * end):catch(function(e)
213
+ * -- Either getting something failed or the time was exceeded.
214
+ *
215
+ * if Promise.Error.isKind(e, Promise.Error.Kind.TimedOut) then
216
+ * warn("Operation timed out!")
217
+ * else
218
+ * warn("Operation encountered an error!")
219
+ * end
220
+ * end)
221
+ * ```
222
+ * Sugar for:
223
+ * ```lua
224
+ * Promise.race({
225
+ * Promise.delay(seconds):andThen(function()
226
+ * return Promise.reject(rejectionValue == nil and Promise.Error.new({ kind = Promise.Error.Kind.TimedOut }) or rejectionValue)
227
+ * end),
228
+ * promise
229
+ * })
230
+ * ```
231
+ */
232
+ timeout(this: Promise<T>, seconds: number, rejectionValue?: any): Promise<T>;
233
+
234
+ /**
235
+ * Cancels this promise, preventing the promise from resolving or rejecting. Does not do anything if the promise is already settled.
236
+ *
237
+ * Cancellations will propagate upwards and downwards through chained promises.
238
+ *
239
+ * Promises will only be cancelled if all of their consumers are also cancelled. This is to say that if you call `andThen` twice on the same promise, and you cancel only one of the child promises, it will not cancel the parent promise until the other child promise is also cancelled.
240
+ * ```lua
241
+ * promise:cancel()
242
+ * ```
243
+ */
244
+ cancel(this: Promise<T>): void;
245
+
246
+ /**
247
+ * Chains a Promise from this one that is resolved if this Promise is already resolved, and rejected if it is not resolved at the time of calling `:now()`. This can be used to ensure your `andThen` handler occurs on the same frame as the root Promise execution.
248
+ * ```lua
249
+ * doSomething()
250
+ * :now()
251
+ * :andThen(function(value)
252
+ * print("Got", value, "synchronously.")
253
+ * end)
254
+ * ```
255
+ * If this Promise is still running, Rejected, or Cancelled, the Promise returned from `:now()` will reject with the `rejectionValue` if passed, otherwise with a `Promise.Error(Promise.Error.Kind.NotResolvedInTime)`. This can be checked with `Error.isKind`.
256
+ */
257
+ now(this: Promise<T>, rejectionValue?: any): Promise<T>;
258
+
259
+ /**
260
+ * Yields the current thread until the given Promise completes. Returns true if the Promise resolved, followed by the values that the promise resolved or rejected with.
261
+ * > If the Promise gets cancelled, this function will return `false`, which is indistinguishable from a rejection. If you need to differentiate, you should use [Promise.awaitStatus](https://eryn.io/roblox-lua-promise/lib/#awaitstatus) instead.
262
+ */
263
+ await(this: Promise<T>): LuaTuple<[true, T] | [false, unknown]>;
264
+
265
+ /**
266
+ * Yields the current thread until the given Promise completes. Returns the Promise's status, followed by the values that the promise resolved or rejected with.
267
+ */
268
+ awaitStatus(this: Promise<T>): LuaTuple<[Promise.Status, unknown]>;
269
+
270
+ /**
271
+ * Yields the current thread until the given Promise completes. Returns the values that the promise resolved with.
272
+ * ```lua
273
+ * local worked = pcall(function()
274
+ * print("got", getTheValue():expect())
275
+ * end)
276
+ *
277
+ * if not worked then
278
+ * warn("it failed")
279
+ * end
280
+ * ```
281
+ * This is essentially sugar for:
282
+ * ```lua
283
+ * select(2, assert(promise:await()))
284
+ * ```
285
+ * **Errors** if the Promise rejects or gets cancelled.
286
+ */
287
+ expect(this: Promise<T>): T;
288
+
289
+ /** Returns the current Promise status. */
290
+ getStatus(this: Promise<T>): Promise.Status;
291
+ }
292
+
293
+ interface PromiseConstructor {
294
+ readonly Status: {
295
+ /** The Promise is executing, and not settled yet. */
296
+ readonly Started: "Started";
297
+ /** The Promise finished successfully. */
298
+ readonly Resolved: "Resolved";
299
+ /** The Promise was rejected. */
300
+ readonly Rejected: "Rejected";
301
+ /** The Promise was cancelled before it finished. */
302
+ readonly Cancelled: "Cancelled";
303
+ };
304
+
305
+ readonly Error: Promise.ErrorConstructor;
306
+
307
+ /**
308
+ * Construct a new Promise that will be resolved or rejected with the given callbacks.
309
+ *
310
+ * If you `resolve` with a Promise, it will be chained onto.
311
+ *
312
+ * You can safely yield within the executor function and it will not block the creating thread.
313
+ *
314
+ * ```lua
315
+ * local myFunction()
316
+ * return Promise.new(function(resolve, reject, onCancel)
317
+ * wait(1)
318
+ * resolve("Hello world!")
319
+ * end)
320
+ * end
321
+ *
322
+ * myFunction():andThen(print)
323
+ * ```
324
+ * You do not need to use `pcall` within a Promise. Errors that occur during execution will be caught and turned into a rejection automatically. If error() is called with a table, that table will be the rejection value. Otherwise, string errors will be converted into `Promise.Error(Promise.Error.Kind.ExecutionError)` objects for tracking debug information.
325
+ *
326
+ * You may register an optional cancellation hook by using the `onCancel` argument:
327
+ * - This should be used to abort any ongoing operations leading up to the promise being settled.
328
+ * - Call the `onCancel` function with a function callback as its only argument to set a hook which will in turn be called when/if the promise is cancelled.
329
+ * - `onCancel` returns `true` if the Promise was already cancelled when you called `onCancel`.
330
+ * - Calling `onCancel` with no argument will not override a previously set cancellation hook, but it will still return `true` if the Promise is currently cancelled.
331
+ * - You can set the cancellation hook at any time before resolving.
332
+ * - When a promise is cancelled, calls to `resolve` or `reject` will be ignored, regardless of if you set a cancellation hook or not.
333
+ */
334
+ new <T>(
335
+ executor: (
336
+ resolve: (value: T | Promise<T>) => void,
337
+ reject: (reason?: unknown) => void,
338
+ onCancel: (abortHandler?: () => void) => boolean,
339
+ ) => void,
340
+ ): Promise<T>;
341
+
342
+ /**
343
+ * The same as [Promise.new](https://eryn.io/roblox-lua-promise/lib/#new), except execution begins after the next `Heartbeat` event.
344
+ *
345
+ * This is a spiritual replacement for `spawn`, but it does not suffer from the same issues as `spawn`.
346
+ *
347
+ * ```lua
348
+ * local function waitForChild(instance, childName, timeout)
349
+ * return Promise.defer(function(resolve, reject)
350
+ * local child = instance:WaitForChild(childName, timeout)
351
+ * if child then
352
+ * resolve(child)
353
+ * else
354
+ * reject(child)
355
+ * end
356
+ * end)
357
+ * end
358
+ * ```
359
+ */
360
+ defer: <T>(
361
+ executor: (
362
+ resolve: (value: T | [T] | [Promise<T>]) => void,
363
+ reject: (reason?: unknown) => void,
364
+ onCancel: (abortHandler?: () => void) => boolean,
365
+ ) => void,
366
+ ) => Promise<T>;
367
+
368
+ /**
369
+ * Begins a Promise chain, calling a function and returning a Promise resolving with its return value. If the function errors, the returned Promise will be rejected with the error. You can safely yield within the Promise.try callback.
370
+ *
371
+ * > `Promise.try` is similar to [Promise.promisify](https://eryn.io/roblox-lua-promise/lib/#promisify), except the callback is invoked immediately instead of returning a new function.
372
+ */
373
+ try: <T>(callback: () => T) => Promise<T>;
374
+
375
+ /**
376
+ * Wraps a function that yields into one that returns a Promise.
377
+ *
378
+ * Any errors that occur while executing the function will be turned into rejections.
379
+ *
380
+ * > `Promise.promisify` is similar to [Promise.try](https://eryn.io/roblox-lua-promise/lib/#try), except the callback is returned as a callable function instead of being invoked immediately.
381
+ */
382
+ promisify: <T extends Array<any>, U>(callback: (...args: T) => U) => (...args: T) => Promise<U>;
383
+
384
+ /** Creates an immediately resolved Promise with the given value. */
385
+ resolve(this: void): Promise<void>;
386
+ resolve<T>(this: void, value: T): Promise<T>;
387
+
388
+ /**
389
+ * Creates an immediately rejected Promise with the given value.
390
+ *
391
+ * > Someone needs to consume this rejection (i.e. `:catch()` it), otherwise it will emit an unhandled Promise rejection warning on the next frame. Thus, you should not create and store rejected Promises for later use. Only create them on-demand as needed.
392
+ */
393
+ reject: (value: unknown) => Promise<unknown>;
394
+
395
+ /**
396
+ * Accepts an array of Promises and returns a new promise that:
397
+ * - is resolved after all input promises resolve.
398
+ * - is rejected if _any_ input promises reject.
399
+ *
400
+ * Note: Only the first return value from each promise will be present in the resulting array.
401
+ *
402
+ * After any input Promise rejects, all other input Promises that are still pending will be cancelled if they have no other consumers.
403
+ *
404
+ * ```lua
405
+ * local promises = {
406
+ * returnsAPromise("example 1"),
407
+ * returnsAPromise("example 2"),
408
+ * returnsAPromise("example 3"),
409
+ * }
410
+ *
411
+ * return Promise.all(promises)
412
+ * ```
413
+ */
414
+ all: <T extends Array<unknown>>(values: readonly [...T]) => Promise<{ [P in keyof T]: Awaited<T[P]> }>;
415
+
416
+ /**
417
+ * Accepts an array of Promises and returns a new Promise that resolves with an array of in-place Statuses when all input Promises have settled. This is equivalent to mapping `promise:finally` over the array of Promises.
418
+ *
419
+ * ```lua
420
+ * local promises = {
421
+ * returnsAPromise("example 1"),
422
+ * returnsAPromise("example 2"),
423
+ * returnsAPromise("example 3"),
424
+ * }
425
+ *
426
+ * return Promise.allSettled(promises)
427
+ * ```
428
+ */
429
+ allSettled: <T>(promises: Array<Promise<T>>) => Promise<Array<Promise.Status>>;
430
+
431
+ /**
432
+ * Accepts an array of Promises and returns a new promise that is resolved or rejected as soon as any Promise in the array resolves or rejects.
433
+ *
434
+ * > If the first Promise to settle from the array settles with a rejection, the resulting Promise from race will reject.
435
+ * > If you instead want to tolerate rejections, and only care about at least one Promise resolving, you should use [Promise.any](https://eryn.io/roblox-lua-promise/lib/#any) or [Promise.some](https://eryn.io/roblox-lua-promise/lib/#some) instead.
436
+ *
437
+ * All other Promises that don't win the race will be cancelled if they have no other consumers.
438
+ *
439
+ * ```lua
440
+ * local promises = {
441
+ * returnsAPromise("example 1"),
442
+ * returnsAPromise("example 2"),
443
+ * returnsAPromise("example 3"),
444
+ * }
445
+ *
446
+ * return Promise.race(promises)
447
+ * ```
448
+ */
449
+ race: <T>(promises: Array<Promise<T>>) => Promise<T>;
450
+
451
+ /**
452
+ * Accepts an array of Promises and returns a Promise that is resolved as soon as `count` Promises are resolved from the input array. The resolved array values are in the order that the Promises resolved in. When this Promise resolves, all other pending Promises are cancelled if they have no other consumers.
453
+ *
454
+ * `count` 0 results in an empty array. The resultant array will never have more than count elements.
455
+ *
456
+ * ```lua
457
+ * local promises = {
458
+ * returnsAPromise("example 1"),
459
+ * returnsAPromise("example 2"),
460
+ * returnsAPromise("example 3"),
461
+ * }
462
+ *
463
+ * return Promise.some(promises, 2) -- Only resolves with first 2 promises to resolve
464
+ * ```
465
+ */
466
+ some: <T>(promises: Array<Promise<T>>, count: number) => Promise<Array<T>>;
467
+
468
+ /**
469
+ * Accepts an array of Promises and returns a Promise that is resolved as soon as _any_ of the input Promises resolves. It will reject only if _all_ input Promises reject. As soon as one Promises resolves, all other pending Promises are cancelled if they have no other consumers.
470
+ *
471
+ * Resolves directly with the value of the first resolved Promise. This is essentially [Promise.some](https://eryn.io/roblox-lua-promise/lib/#some) with `1` count, except the Promise resolves with the value directly instead of an array with one element.
472
+ * ```lua
473
+ * local promises = {
474
+ * returnsAPromise("example 1"),
475
+ * returnsAPromise("example 2"),
476
+ * returnsAPromise("example 3"),
477
+ * }
478
+ *
479
+ * return Promise.any(promises) -- Resolves with first value to resolve (only rejects if all 3 rejected)
480
+ * ```
481
+ */
482
+ any: <T>(promises: Array<Promise<T>>) => Promise<T>;
483
+
484
+ /**
485
+ * Returns a Promise that resolves after `seconds` seconds have passed. The Promise resolves with the actual amount of time that was waited.
486
+ *
487
+ * This function is **not** a wrapper around `wait`. `Promise.delay` uses a custom scheduler which provides more accurate timing. As an optimization, cancelling this Promise instantly removes the task from the scheduler.
488
+ *
489
+ * > Passing `NaN`, infinity, or a number less than 1/60 is equivalent to passing 1/60.
490
+ */
491
+ delay: (seconds: number) => Promise<number>;
492
+
493
+ /**
494
+ * Iterates serially over the given an array of values, calling the predicate callback on each value before continuing.
495
+ *
496
+ * If the predicate returns a Promise, we wait for that Promise to resolve before moving on to the next item in the array.
497
+ *
498
+ * > `Promise.each` is similar to `Promise.all`, except the Promises are ran in order instead of all at once.
499
+ * > But because Promises are eager, by the time they are created, they're already running. Thus, we need a way to defer creation of each Promise until a later time.
500
+ * > The predicate function exists as a way for us to operate on our data instead of creating a new closure for each Promise. If you would prefer, you can pass in an array of functions, and in the predicate, call the function and return its return value.
501
+ *
502
+ * ```lua
503
+ * Promise.each({
504
+ * "foo",
505
+ * "bar",
506
+ * "baz",
507
+ * "qux"
508
+ * }, function(value, index)
509
+ * return Promise.delay(1):andThen(function()
510
+ * print(("%d) Got %s!"):format(index, value))
511
+ * end)
512
+ * end)
513
+ *
514
+ * --[[
515
+ * (1 second passes)
516
+ * > 1) Got foo!
517
+ * (1 second passes)
518
+ * > 2) Got bar!
519
+ * (1 second passes)
520
+ * > 3) Got baz!
521
+ * (1 second passes)
522
+ * > 4) Got qux!
523
+ * ]]
524
+ * ```
525
+ *
526
+ * If the Promise a predicate returns rejects, the Promise from `Promise.each` is also rejected with the same value.
527
+ *
528
+ * If the array of values contains a Promise, when we get to that point in the list, we wait for the Promise to resolve before calling the predicate with the value.
529
+ *
530
+ * If a Promise in the array of values is already Rejected when `Promise.each` is called, `Promise.each` rejects with that value immediately (the predicate callback will never be called even once). If a Promise in the list is already Cancelled when `Promise.each` is called, `Promise.each` rejects with `Promise.Error(Promise.Error.Kind.AlreadyCancelled)`. If a Promise in the array of values is Started at first, but later rejects, `Promise.each` will reject with that value and iteration will not continue once iteration encounters that value.
531
+ *
532
+ * Returns a Promise containing an array of the returned/resolved values from the predicate for each item in the array of values.
533
+ *
534
+ * If this Promise returned from `Promise.each` rejects or is cancelled for any reason, the following are true:
535
+ * - Iteration will not continue.
536
+ * - Any Promises within the array of values will now be cancelled if they have no other consumers.
537
+ * - The Promise returned from the currently active predicate will be cancelled if it hasn't resolved yet.
538
+ */
539
+ each: <T, U>(
540
+ list: Array<T | Promise<T>>,
541
+ predicate: (value: T, index: number) => U | Promise<U>,
542
+ ) => Promise<Array<U>>;
543
+
544
+ /**
545
+ * Repeatedly calls a Promise-returning function up to `times` number of times, until the returned Promise resolves.
546
+ *
547
+ * If the amount of retries is exceeded, the function will return the latest rejected Promise.
548
+ * ```lua
549
+ * local function canFail(a, b, c)
550
+ * return Promise.new(function(resolve, reject)
551
+ * -- do something that can fail
552
+ *
553
+ * local failed, thing = doSomethingThatCanFail(a, b, c)
554
+ *
555
+ * if failed then
556
+ * reject("it failed")
557
+ * else
558
+ * resolve(thing)
559
+ * end
560
+ * end)
561
+ * end
562
+ *
563
+ * local MAX_RETRIES = 10
564
+ * local value = Promise.retry(canFail, MAX_RETRIES, "foo", "bar", "baz") -- args to send to canFail
565
+ * ```
566
+ */
567
+ retry: <P extends Array<any>, T>(callback: (...args: P) => Promise<T>, times: number, ...args: P) => Promise<T>;
568
+
569
+ /**
570
+ * Repeatedly calls a Promise-returning function up to `times` number of times, waiting `seconds` seconds between
571
+ * each retry, until the returned Promise resolves.
572
+ *
573
+ * If the amount of retries is exceeded, the function will return the latest rejected Promise.
574
+ */
575
+ retryWithDelay: <P extends Array<any>, T>(
576
+ callback: (...args: P) => Promise<T>,
577
+ times: number,
578
+ seconds: number,
579
+ ...args: P
580
+ ) => Promise<T>;
581
+
582
+ /**
583
+ * Converts an event into a Promise which resolves the next time the event fires.
584
+ *
585
+ * The optional `predicate` callback, if passed, will receive the event arguments and should return `true` or `false`, based on if this fired event should resolve the Promise or not. If `true`, the Promise resolves. If `false`, nothing happens and the predicate will be rerun the next time the event fires.
586
+ *
587
+ * The Promise will resolve with the event arguments.
588
+ *
589
+ * > This function will work given any object with a `Connect` method. This includes all Roblox events.
590
+ * ```lua
591
+ * -- Creates a Promise which only resolves when `somePart` is touched by a part named `"Something specific"`.
592
+ * return Promise.fromEvent(somePart.Touched, function(part)
593
+ * return part.Name == "Something specific"
594
+ * end)
595
+ * ```
596
+ */
597
+ fromEvent<T>(this: void, event: RBXScriptSignal<(value: T) => void>, predicate?: (value: T) => boolean): Promise<T>;
598
+ fromEvent(this: void, event: RBXScriptSignal<() => void>, predicate?: () => boolean): Promise<void>;
599
+ fromEvent<T>(
600
+ this: void,
601
+ event: { Connect: (callback: (value: T) => void) => void },
602
+ predicate?: (value: T) => boolean,
603
+ ): Promise<T>;
604
+
605
+ /** Checks whether the given object is a Promise via duck typing. This only checks if the object is a table and has an `andThen` method. */
606
+ is: (object: unknown) => object is Promise<unknown>;
607
+
608
+ /**
609
+ * Folds an array of values or promises into a single value. The array is traversed sequentially.
610
+ *
611
+ * The reducer function can return a promise or value directly. Each iteration receives the resolved value from the previous, and the first receives your defined initial value.
612
+ *
613
+ * The folding will stop at the first rejection encountered.
614
+ * ```lua
615
+ * local basket = {"blueberry", "melon", "pear", "melon"}
616
+ * Promise.fold(basket, function(cost, fruit)
617
+ * if fruit == "blueberry" then
618
+ * return cost -- blueberries are free!
619
+ * else
620
+ * -- call a function that returns a promise with the fruit price
621
+ * return fetchPrice(fruit):andThen(function(fruitCost)
622
+ * return cost + fruitCost
623
+ * end)
624
+ * end
625
+ * end, 0)
626
+ * ```
627
+ */
628
+ fold: <T, U>(
629
+ list: Array<T | Promise<T>>,
630
+ reducer: (accumulator: U, value: T, index: number) => U | Promise<U>,
631
+ initialValue: U,
632
+ ) => Promise<U>;
633
+
634
+ /**
635
+ * Registers a callback that runs when an unhandled rejection happens. An unhandled rejection happens when a Promise
636
+ * is rejected, and the rejection is not observed with `:catch`.
637
+ *
638
+ * The callback is called with the actual promise that rejected, followed by the rejection values.
639
+ */
640
+ onUnhandledRejection: (callback: (this: Promise<never>, ...values: Array<unknown>) => void) => () => void;
641
+ }
642
+
643
+ declare namespace Promise {
644
+ export type Status = PromiseConstructor["Status"][keyof PromiseConstructor["Status"]];
645
+ }
646
+
647
+ declare const Promise: PromiseConstructor;