-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
future.dart
1361 lines (1319 loc) · 51.5 KB
/
future.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
part of dart.async;
/// A type representing values that are either `Future<T>` or `T`.
///
/// This class declaration is a public stand-in for an internal
/// future-or-value generic type, which is not a class type.
/// References to this class are resolved to the internal type.
///
/// It is a compile-time error for any class to extend, mix in or implement
/// `FutureOr`.
///
/// ### Examples
///
/// ```dart
/// // The `Future<T>.then` function takes a callback [f] that returns either
/// // an `S` or a `Future<S>`.
/// Future<S> then<S>(FutureOr<S> f(T x), ...);
///
/// // `Completer<T>.complete` takes either a `T` or `Future<T>`.
/// void complete(FutureOr<T> value);
/// ```
///
/// ### Advanced
///
/// The `FutureOr<int>` type is actually the "type union" of the types `int` and
/// `Future<int>`. This type union is defined in such a way that
/// `FutureOr<Object>` is both a super- and sub-type of `Object` (sub-type
/// because `Object` is one of the types of the union, super-type because
/// `Object` is a super-type of both of the types of the union). Together it
/// means that `FutureOr<Object>` is equivalent to `Object`.
///
/// As a corollary, `FutureOr<Object>` is equivalent to
/// `FutureOr<FutureOr<Object>>`, `FutureOr<Future<Object>>` is equivalent to
/// `Future<Object>`.
@pragma("vm:entry-point")
abstract class FutureOr<T> {
// Private generative constructor, so that it is not subclassable, mixable,
// or instantiable.
FutureOr._() {
throw new UnsupportedError("FutureOr cannot be instantiated");
}
}
/// The result of an asynchronous computation.
///
/// An _asynchronous computation_ cannot provide a result immediately
/// when it is started, unlike a synchronous computation which does compute
/// a result immediately by either returning a value or by throwing.
/// An asynchronous computation may need to wait for something external
/// to the program (reading a file, querying a database, fetching a web page)
/// which takes time.
/// Instead of blocking all computation until the result is available,
/// the asynchronous computation immediately returns a `Future`
/// which will *eventually* "complete" with the result.
///
/// ### Asynchronous programming
///
/// To perform an asynchronous computation, you use an `async` function
/// which always produces a future.
/// Inside such an asynchronous function, you can use the `await` operation
/// to delay execution until another asynchronous computation has a result.
/// While execution of the awaiting function is delayed,
/// the program is not blocked, and can continue doing other things.
///
/// Example:
/// ```dart
/// import "dart:io";
/// Future<bool> fileContains(String path, String needle) async {
/// var haystack = await File(path).readAsString();
/// return haystack.contains(needle);
/// }
/// ```
/// Here the `File.readAsString` method from `dart:io` is an asynchronous
/// function returning a `Future<String>`.
/// The `fileContains` function is marked with `async` right before its body,
/// which means that you can use `await` inside it,
/// and that it must return a future.
/// The call to `File(path).readAsString()` initiates reading the file into
/// a string and produces a `Future<String>` which will eventually contain the
/// result.
/// The `await` then waits for that future to complete with a string
/// (or an error, if reading the file fails).
/// While waiting, the program can do other things.
/// When the future completes with a string, the `fileContains` function
/// computes a boolean and returns it, which then completes the original
/// future that it returned when first called.
///
/// If a future completes with an *error*, awaiting that future will
/// (re-)throw that error. In the example here, we can add error checking:
/// ```dart
/// import "dart:io";
/// Future<bool> fileContains(String path, String needle) async {
/// try {
/// var haystack = await File(path).readAsString();
/// return haystack.contains(needle);
/// } on FileSystemException catch (exception, stack) {
/// _myLog.logError(exception, stack);
/// return false;
/// }
/// }
/// ```
/// You use a normal `try`/`catch` to catch the failures of awaited
/// asynchronous computations.
///
/// In general, when writing asynchronous code, you should always await a
/// future when it is produced, and not wait until after another asynchronous
/// delay. That ensures that you are ready to receive any error that the
/// future might produce, which is important because an asynchronous error
/// that no-one is awaiting is an *uncaught* error and may terminate
/// the running program.
///
/// ### Programming with the `Future` API.
///
/// The `Future` class also provides a more direct, low-level functionality
/// for accessing the result that it completes with.
/// The `async` and `await` language features are built on top of this
/// functionality, and it sometimes makes sense to use it directly.
/// There are things that you cannot do by just `await`ing one future at
/// a time.
///
/// With a [Future], you can manually register callbacks
/// that handle the value, or error, once it is available.
/// For example:
/// ```dart
/// Future<int> future = getFuture();
/// future.then((value) => handleValue(value))
/// .catchError((error) => handleError(error));
/// ```
/// Since a [Future] can be completed in two ways,
/// either with a value (if the asynchronous computation succeeded)
/// or with an error (if the computation failed),
/// you can install callbacks for either or both cases.
///
/// In some cases we say that a future is completed *with another future*.
/// This is a short way of stating that the future is completed in the same way,
/// with the same value or error,
/// as the other future once that other future itself completes.
/// Most functions in the platform libraries that complete a future
/// (for example [Completer.complete] or [Future.value]),
/// also accepts another future, and automatically handles forwarding
/// the result to the future being completed.
///
/// The result of registering callbacks is itself a `Future`,
/// which in turn is completed with the result of invoking the
/// corresponding callback with the original future's result.
/// The new future is completed with an error if the invoked callback throws.
/// For example:
/// ```dart
/// Future<int> successor = future.then((int value) {
/// // Invoked when the future is completed with a value.
/// return 42; // The successor is completed with the value 42.
/// },
/// onError: (e) {
/// // Invoked when the future is completed with an error.
/// if (canHandle(e)) {
/// return 499; // The successor is completed with the value 499.
/// } else {
/// throw e; // The successor is completed with the error e.
/// }
/// });
/// ```
///
/// If a future does not have any registered handler when it completes
/// with an error, it forwards the error to an "uncaught-error handler".
/// This behavior ensures that no error is silently dropped.
/// However, it also means that error handlers should be installed early,
/// so that they are present as soon as a future is completed with an error.
/// The following example demonstrates this potential bug:
/// ```dart
/// var future = getFuture();
/// Timer(const Duration(milliseconds: 5), () {
/// // The error-handler is not attached until 5 ms after the future has
/// // been received. If the future fails before that, the error is
/// // forwarded to the global error-handler, even though there is code
/// // (just below) to eventually handle the error.
/// future.then((value) { useValue(value); },
/// onError: (e) { handleError(e); });
/// });
/// ```
///
/// When registering callbacks, it's often more readable to register the two
/// callbacks separately, by first using [then] with one argument
/// (the value handler) and using a second [catchError] for handling errors.
/// Each of these will forward the result that they don't handle
/// to their successors, and together they handle both value and error result.
/// It has the additional benefit of the [catchError] handling errors in the
/// [then] value callback too.
/// Using sequential handlers instead of parallel ones often leads to code that
/// is easier to reason about.
/// It also makes asynchronous code very similar to synchronous code:
/// ```dart
/// // Synchronous code.
/// try {
/// int value = foo();
/// return bar(value);
/// } catch (e) {
/// return 499;
/// }
/// ```
///
/// Equivalent asynchronous code, based on futures:
/// ```dart
/// Future<int> asyncValue = Future(foo); // Result of foo() as a future.
/// asyncValue.then((int value) {
/// return bar(value);
/// }).catchError((e) {
/// return 499;
/// });
/// ```
///
/// Similar to the synchronous code, the error handler (registered with
/// [catchError]) is handling any errors thrown by either `foo` or `bar`.
/// If the error-handler had been registered as the `onError` parameter of
/// the `then` call, it would not catch errors from the `bar` call.
///
/// Futures can have more than one callback-pair registered. Each successor is
/// treated independently and is handled as if it was the only successor.
/// The order in which the individual successors are completed is undefined.
///
/// A future may also fail to ever complete. In that case, no callbacks are
/// called. That situation should generally be avoided if possible, unless
/// it's very clearly documented.
@pragma("wasm:entry-point")
@vmIsolateUnsendable
abstract interface class Future<T> {
/// A `Future<Null>` completed with `null`.
///
/// Currently shared with `dart:internal`.
/// If that future can be removed, then change this back to
/// `_Future<Null>.zoneValue(null, _rootZone);`
static final _Future<Null> _nullFuture = nullFuture as _Future<Null>;
/// A `Future<bool>` completed with `false`.
static final _Future<bool> _falseFuture =
new _Future<bool>.zoneValue(false, _rootZone);
/// Creates a future containing the result of calling [computation]
/// asynchronously with [Timer.run].
///
/// If the result of executing [computation] throws, the returned future is
/// completed with the error.
///
/// If the returned value is itself a [Future], completion of
/// the created future will wait until the returned future completes,
/// and will then complete with the same result.
///
/// If a non-future value is returned, the returned future is completed
/// with that value.
factory Future(FutureOr<T> computation()) {
_Future<T> result = new _Future<T>();
Timer.run(() {
FutureOr<T> computationResult;
try {
computationResult = computation();
} catch (e, s) {
_completeWithErrorCallback(result, e, s);
return;
}
result._complete(computationResult);
});
return result;
}
/// Creates a future containing the result of calling [computation]
/// asynchronously with [scheduleMicrotask].
///
/// If executing [computation] throws,
/// the returned future is completed with the thrown error.
///
/// If calling [computation] returns a [Future], completion of
/// the created future will wait until the returned future completes,
/// and will then complete with the same result.
///
/// If calling [computation] returns a non-future value,
/// the returned future is completed with that value.
factory Future.microtask(FutureOr<T> computation()) {
_Future<T> result = new _Future<T>();
scheduleMicrotask(() {
FutureOr<T> computationResult;
try {
computationResult = computation();
} catch (e, s) {
_completeWithErrorCallback(result, e, s);
return;
}
result._complete(computationResult);
});
return result;
}
/// Returns a future containing the result of immediately calling
/// [computation].
///
/// If calling [computation] throws, the returned future is completed with the
/// error.
///
/// If calling [computation] returns a `Future<T>`, that future is returned.
///
/// If calling [computation] returns a non-future value,
/// a future is returned which has been completed with that value.
///
/// Example:
/// ```dart
/// final result = await Future<int>.sync(() => 12);
/// ```
factory Future.sync(FutureOr<T> computation()) {
FutureOr<T> result;
try {
result = computation();
} catch (error, stackTrace) {
var future = new _Future<T>();
AsyncError? replacement = Zone.current.errorCallback(error, stackTrace);
if (replacement != null) {
future._asyncCompleteError(replacement.error, replacement.stackTrace);
} else {
future._asyncCompleteError(error, stackTrace);
}
return future;
}
return result is Future<T> ? result : _Future<T>.value(result);
}
/// Creates a future completed with [value].
///
/// If [value] is a future, the created future waits for the
/// [value] future to complete, and then completes with the same result.
/// Since a [value] future can complete with an error, so can the future
/// created by [Future.value], even if the name suggests otherwise.
///
/// If [value] is not a [Future], the created future is completed
/// with the [value] value,
/// equivalently to `new Future<T>.sync(() => value)`.
///
/// If [value] is omitted or `null`, it is converted to `FutureOr<T>` by
/// `value as FutureOr<T>`. If `T` is not nullable, then a non-`null` [value]
/// must be provided, otherwise the construction throws.
///
/// Use [Completer] to create a future now and complete it later.
///
/// Example:
/// ```dart
/// Future<int> getFuture() {
/// return Future<int>.value(2021);
/// }
///
/// final result = await getFuture();
/// ```
@pragma("vm:entry-point")
@pragma("vm:prefer-inline")
factory Future.value([FutureOr<T>? value]) {
return new _Future<T>.immediate(value == null ? value as T : value);
}
/// Creates a future that completes with an error.
///
/// The created future will be completed with an error in a future microtask.
/// This allows enough time for someone to add an error handler on the future.
/// If an error handler isn't added before the future completes, the error
/// will be considered unhandled.
///
/// Use [Completer] to create a future and complete it later.
///
/// Example:
/// ```dart
/// Future<int> getFuture() {
/// return Future.error(Exception('Issue'));
/// }
///
/// final error = await getFuture(); // Throws.
/// ```
factory Future.error(Object error, [StackTrace? stackTrace]) {
// TODO(40614): Remove once non-nullability is sound.
checkNotNullable(error, "error");
if (!identical(Zone.current, _rootZone)) {
AsyncError? replacement = Zone.current.errorCallback(error, stackTrace);
if (replacement != null) {
error = replacement.error;
stackTrace = replacement.stackTrace;
}
}
stackTrace ??= AsyncError.defaultStackTrace(error);
return new _Future<T>.immediateError(error, stackTrace);
}
/// Creates a future that runs its computation after a delay.
///
/// The [computation] will be executed after the given [duration] has passed,
/// and the future is completed with the result of the computation.
///
/// If [computation] returns a future,
/// the future returned by this constructor will complete with the value or
/// error of that future.
///
/// If the duration is 0 or less,
/// it completes no sooner than in the next event-loop iteration,
/// after all microtasks have run.
///
/// If [computation] is omitted,
/// it will be treated as if [computation] was `() => null`,
/// and the future will eventually complete with the `null` value.
/// In that case, [T] must be nullable.
///
/// If calling [computation] throws, the created future will complete with the
/// error.
///
/// See also [Completer] for a way to create and complete a future at a
/// later time that isn't necessarily after a known fixed duration.
///
/// Example:
/// ```dart
/// Future.delayed(const Duration(seconds: 1), () {
/// print('One second has passed.'); // Prints after 1 second.
/// });
/// ```
factory Future.delayed(Duration duration, [FutureOr<T> computation()?]) {
if (computation == null && !typeAcceptsNull<T>()) {
throw ArgumentError.value(
null, "computation", "The type parameter is not nullable");
}
_Future<T> result = _Future<T>();
new Timer(duration, () {
if (computation == null) {
result._complete(null as T);
} else {
FutureOr<T> computationResult;
try {
computationResult = computation();
} catch (e, s) {
_completeWithErrorCallback(result, e, s);
return;
}
result._complete(computationResult);
}
});
return result;
}
/// Waits for multiple futures to complete and collects their results.
///
/// Returns a future which will complete once all the provided futures
/// have completed, either with their results, or with an error if any
/// of the provided futures fail.
///
/// The value of the returned future will be a list of all the values that
/// were produced in the order that the futures are provided by iterating
/// [futures].
///
/// If any future completes with an error,
/// then the returned future completes with that error.
/// If further futures also complete with errors, those errors are discarded.
///
/// If `eagerError` is true, the returned future completes with an error
/// immediately on the first error from one of the futures. Otherwise all
/// futures must complete before the returned future is completed (still with
/// the first error; the remaining errors are silently dropped).
///
/// In the case of an error, [cleanUp] (if provided), is invoked on any
/// non-null result of successful futures.
/// This makes it possible to `cleanUp` resources that would otherwise be
/// lost (since the returned future does not provide access to these values).
/// The [cleanUp] function is unused if there is no error.
///
/// The call to [cleanUp] should not throw. If it does, the error will be an
/// uncaught asynchronous error.
///
/// Example:
/// ```dart
/// void main() async {
/// var value = await Future.wait([delayedNumber(), delayedString()]);
/// print(value); // [2, result]
/// }
///
/// Future<int> delayedNumber() async {
/// await Future.delayed(const Duration(seconds: 2));
/// return 2;
/// }
///
/// Future<String> delayedString() async {
/// await Future.delayed(const Duration(seconds: 2));
/// return 'result';
/// }
/// ```
static Future<List<T>> wait<T>(Iterable<Future<T>> futures,
{bool eagerError = false, void cleanUp(T successValue)?}) {
@pragma('vm:awaiter-link')
final _Future<List<T>> _future = _Future<List<T>>();
List<T?>? values; // Collects the values. Set to null on error.
int remaining = 0; // How many futures are we waiting for.
Object? error; // The first error from a future.
StackTrace? stackTrace; // The stackTrace that came with the error.
// Handle an error from any of the futures.
void handleError(Object theError, StackTrace theStackTrace) {
var remainingResults = --remaining;
List<T?>? valueList = values;
if (valueList != null) {
// First error, set state to represent error having already happened.
values = null;
error = theError;
stackTrace = theStackTrace;
// Then clean up any already successfully produced results.
if (cleanUp != null) {
for (var value in valueList) {
if (value != null) {
// Ensure errors from `cleanUp` are uncaught.
T cleanUpValue = value;
Future.sync(() {
cleanUp(cleanUpValue);
});
}
}
}
if (remainingResults == 0 || eagerError) {
_future._completeError(theError, theStackTrace);
}
} else {
// Not the first error.
if (remainingResults == 0 && !eagerError) {
// Last future completed, non-eagerly report the first error.
_future._completeError(error!, stackTrace!);
}
}
}
try {
// As each future completes, put its value into the corresponding
// position in the list of values.
for (var future in futures) {
int pos = remaining;
future.then((T value) {
var remainingResults = --remaining;
List<T?>? valueList = values;
if (valueList != null) {
// No errors yet.
assert(valueList[pos] == null);
valueList[pos] = value;
if (remainingResults == 0) {
_future._completeWithValue(
[for (var value in valueList) value as T]);
}
} else {
// Prior error, clean-up this value if necessary.
if (cleanUp != null && value != null) {
// Ensure errors from cleanUp are uncaught.
Future.sync(() {
cleanUp(value);
});
}
if (remainingResults == 0 && !eagerError) {
// Last future completed, non-eagerly report the first error.
_future._completeError(error!, stackTrace!);
}
}
}, onError: handleError);
// Increment the 'remaining' after the call to 'then'.
// If that call throws, we don't expect any future callback from
// the future, and we also don't increment remaining.
remaining++;
}
if (remaining == 0) {
// No elements in iterable.
return _future.._completeWithValue(<T>[]);
}
values = List<T?>.filled(remaining, null);
} catch (e, st) {
// The error must have been thrown while iterating over the futures
// list, or while installing a callback handler on the future.
// This is a breach of the `Future` protocol, but we try to handle it
// gracefully.
if (remaining == 0 || eagerError) {
// Throw a new Future.error.
// Don't just call `_future._completeError` since that would propagate
// the error too eagerly, not giving the callers time to install
// error handlers.
// Also, don't use `_asyncCompleteError` since that one doesn't give
// zones the chance to intercept the error.
return new Future.error(e, st);
} else {
// Don't allocate a list for values, thus indicating that there was an
// error.
// Set error to the caught exception.
error = e;
stackTrace = st;
}
}
return _future;
}
/// Returns the result of the first future in [futures] to complete.
///
/// The returned future is completed with the result of the first
/// future in [futures] to report that it is complete,
/// whether it's with a value or an error.
/// The results of all the other futures are discarded.
///
/// If [futures] is empty, or if none of its futures complete,
/// the returned future never completes.
///
/// Example:
/// ```dart
/// void main() async {
/// final result =
/// await Future.any([slowInt(), delayedString(), fastInt()]);
/// // The future of fastInt completes first, others are ignored.
/// print(result); // 3
/// }
/// Future<int> slowInt() async {
/// await Future.delayed(const Duration(seconds: 2));
/// return 2;
/// }
///
/// Future<String> delayedString() async {
/// await Future.delayed(const Duration(seconds: 2));
/// throw TimeoutException('Time has passed');
/// }
///
/// Future<int> fastInt() async {
/// await Future.delayed(const Duration(seconds: 1));
/// return 3;
/// }
/// ```
static Future<T> any<T>(Iterable<Future<T>> futures) {
var completer = new Completer<T>.sync();
void onValue(T value) {
if (!completer.isCompleted) completer.complete(value);
}
void onError(Object error, StackTrace stack) {
if (!completer.isCompleted) completer.completeError(error, stack);
}
for (var future in futures) {
future.then(onValue, onError: onError);
}
return completer.future;
}
/// Performs an action for each element of the iterable, in turn.
///
/// The [action] may be either synchronous or asynchronous.
///
/// Calls [action] with each element in [elements] in order.
/// If the call to [action] returns a `Future<T>`, the iteration waits
/// until the future is completed before continuing with the next element.
///
/// Returns a [Future] that completes with `null` when all elements have been
/// processed.
///
/// Non-[Future] return values, and completion-values of returned [Future]s,
/// are discarded.
///
/// Any error from [action], synchronous or asynchronous,
/// will stop the iteration and be reported in the returned [Future].
static Future<void> forEach<T>(
Iterable<T> elements, FutureOr action(T element)) {
var iterator = elements.iterator;
return doWhile(() {
if (!iterator.moveNext()) return false;
var result = action(iterator.current);
if (result is Future) return result.then(_kTrue);
return true;
});
}
// Constant `true` function, used as callback by [forEach].
static bool _kTrue(Object? _) => true;
/// Performs an operation repeatedly until it returns `false`.
///
/// The operation, [action], may be either synchronous or asynchronous.
///
/// The operation is called repeatedly as long as it returns either the [bool]
/// value `true` or a `Future<bool>` which completes with the value `true`.
///
/// If a call to [action] returns `false` or a [Future] that completes to
/// `false`, iteration ends and the future returned by [doWhile] is completed
/// with a `null` value.
///
/// If a call to [action] throws or a future returned by [action] completes
/// with an error, iteration ends and the future returned by [doWhile]
/// completes with the same error.
///
/// Calls to [action] may happen at any time,
/// including immediately after calling `doWhile`.
/// The only restriction is a new call to [action] won't happen before
/// the previous call has returned, and if it returned a `Future<bool>`, not
/// until that future has completed.
///
/// Example:
/// ```dart
/// void main() async {
/// var value = 0;
/// await Future.doWhile(() async {
/// value++;
/// await Future.delayed(const Duration(seconds: 1));
/// if (value == 3) {
/// print('Finished with $value');
/// return false;
/// }
/// return true;
/// });
/// }
/// // Outputs: 'Finished with 3'
/// ```
static Future<void> doWhile(FutureOr<bool> action()) {
_Future<void> doneSignal = new _Future<void>();
late void Function(bool) nextIteration;
// Bind this callback explicitly so that each iteration isn't bound in the
// context of all the previous iterations' callbacks.
// This avoids, e.g., deeply nested stack traces from the stack trace
// package.
nextIteration = Zone.current.bindUnaryCallbackGuarded((bool keepGoing) {
while (keepGoing) {
FutureOr<bool> result;
try {
result = action();
} catch (error, stackTrace) {
// Cannot use _completeWithErrorCallback because it completes
// the future synchronously.
_asyncCompleteWithErrorCallback(doneSignal, error, stackTrace);
return;
}
if (result is Future<bool>) {
result.then(nextIteration, onError: doneSignal._completeError);
return;
}
keepGoing = result;
}
doneSignal._complete(null);
});
nextIteration(true);
return doneSignal;
}
/// Register callbacks to be called when this future completes.
///
/// When this future completes with a value,
/// the [onValue] callback will be called with that value.
/// If this future is already completed, the callback will not be called
/// immediately, but will be scheduled in a later microtask.
///
/// If [onError] is provided, and this future completes with an error,
/// the `onError` callback is called with that error and its stack trace.
/// The `onError` callback must accept either one argument or two arguments
/// where the latter is a [StackTrace].
/// If `onError` accepts two arguments,
/// it is called with both the error and the stack trace,
/// otherwise it is called with just the error object.
/// The `onError` callback must return a value or future that can be used
/// to complete the returned future, so it must be something assignable to
/// `FutureOr<R>`.
///
/// Returns a new [Future]
/// which is completed with the result of the call to `onValue`
/// (if this future completes with a value)
/// or to `onError` (if this future completes with an error).
///
/// If the invoked callback throws,
/// the returned future is completed with the thrown error
/// and a stack trace for the error.
/// In the case of `onError`,
/// if the exception thrown is `identical` to the error argument to `onError`,
/// and it is thrown *synchronously*
/// the throw is considered a rethrow,
/// and the original stack trace is used instead.
/// To rethrow with the same stack trace in an asynchronous callback,
/// use [Error.throwWithStackTrace].
///
/// If the callback returns a [Future],
/// the future returned by `then` will be completed with
/// the same result as the future returned by the callback.
///
/// If [onError] is not given, and this future completes with an error,
/// the error is forwarded directly to the returned future.
///
/// In most cases, it is more readable to use [catchError] separately,
/// possibly with a `test` parameter,
/// instead of handling both value and error in a single [then] call.
///
/// Note that futures don't delay reporting of errors until listeners are
/// added. If the first `then` or `catchError` call happens
/// after this future has completed with an error,
/// then the error is reported as unhandled error.
/// See the description on [Future].
Future<R> then<R>(FutureOr<R> onValue(T value), {Function? onError});
/// Handles errors emitted by this [Future].
///
/// This is the asynchronous equivalent of a "catch" block.
///
/// Returns a new [Future] that will be completed with either the result of
/// this future or the result of calling the `onError` callback.
///
/// If this future completes with a value,
/// the returned future completes with the same value.
///
/// If this future completes with an error,
/// then [test] is first called with the error value.
///
/// If `test` returns false, the exception is not handled by this `catchError`,
/// and the returned future completes with the same error and stack trace
/// as this future.
///
/// If `test` returns `true`,
/// [onError] is called with the error and possibly stack trace,
/// and the returned future is completed with the result of this call
/// in exactly the same way as for [then]'s `onError`.
///
/// If `test` is omitted, it defaults to a function that always returns true.
/// The `test` function should not throw, but if it does, it is handled as
/// if the `onError` function had thrown.
///
/// Note that futures don't delay reporting of errors until listeners are
/// added. If the first `catchError` (or `then`) call happens after this future
/// has completed with an error then the error is reported as unhandled error.
/// See the description on [Future].
///
/// Example:
/// ```dart
/// Future.delayed(
/// const Duration(seconds: 1),
/// () => throw 401,
/// ).then((value) {
/// throw 'Unreachable';
/// }).catchError((err) {
/// print('Error: $err'); // Prints 401.
/// }, test: (error) {
/// return error is int && error >= 400;
/// });
/// ```
// The `Function` below stands for one of two types:
// - (dynamic) -> FutureOr<T>
// - (dynamic, StackTrace) -> FutureOr<T>
// Given that there is a `test` function that is usually used to do an
// `is` check, we should also expect functions that take a specific argument.
Future<T> catchError(Function onError, {bool test(Object error)?});
/// Registers a function to be called when this future completes.
///
/// The [action] function is called when this future completes, whether it
/// does so with a value or with an error.
///
/// This is the asynchronous equivalent of a "finally" block.
///
/// The future returned by this call, `f`, will complete the same way
/// as this future unless an error occurs in the [action] call, or in
/// a [Future] returned by the [action] call. If the call to [action]
/// does not return a future, its return value is ignored.
///
/// If the call to [action] throws, then `f` is completed with the
/// thrown error.
///
/// If the call to [action] returns a [Future], `f2`, then completion of
/// `f` is delayed until `f2` completes. If `f2` completes with
/// an error, that will be the result of `f` too. The value of `f2` is always
/// ignored.
///
/// This method is equivalent to:
/// ```dart
/// Future<T> whenComplete(action()) {
/// return this.then((v) {
/// var f2 = action();
/// if (f2 is Future) return f2.then((_) => v);
/// return v;
/// }, onError: (e) {
/// var f2 = action();
/// if (f2 is Future) return f2.then((_) { throw e; });
/// throw e;
/// });
/// }
/// ```
/// Example:
/// ```dart
/// void main() async {
/// var value =
/// await waitTask().whenComplete(() => print('do something here'));
/// // Prints "do something here" after waitTask() completed.
/// print(value); // Prints "done"
/// }
///
/// Future<String> waitTask() {
/// Future.delayed(const Duration(seconds: 5));
/// return Future.value('done');
/// }
/// // Outputs: 'do some work here' after waitTask is completed.
/// ```
Future<T> whenComplete(FutureOr<void> action());
/// Creates a [Stream] containing the result of this future.
///
/// The stream will produce single data or error event containing the
/// completion result of this future, and then it will close with a
/// done event.
///
/// If the future never completes, the stream will not produce any events.
Stream<T> asStream();
/// Stop waiting for this future after [timeLimit] has passed.
///
/// Creates a new _timeout future_ that completes
/// with the same result as this future, the _source future_,
/// *if* the source future completes in time.
///
/// If the source future does not complete before [timeLimit] has passed,
/// the [onTimeout] action is executed,
/// and its result (whether it returns or throws)
/// is used as the result of the timeout future.
/// The [onTimeout] function must return a [T] or a `Future<T>`.
/// If [onTimeout] returns a future, the _alternative result future_,
/// the eventual result of the alternative result future is used
/// to complete the timeout future,
/// even if the source future completes
/// before the alternative result future.
/// It only matters that the source future did not complete in time.
///
/// If `onTimeout` is omitted, a timeout will cause the returned future to
/// complete with a [TimeoutException].
///
/// In either case, the source future can still complete normally
/// at a later time.
/// It just won't be used as the result of the timeout future
/// unless it completes within the time bound.
/// Even if the source future completes with an error,
/// if that error happens after [timeLimit] has passed,
/// the error is ignored, just like a value result would be.
///
/// Examples:
/// ```dart
/// void main() async {
/// var result =
/// await waitTask("completed").timeout(const Duration(seconds: 10));
/// print(result); // Prints "completed" after 5 seconds.
///
/// result = await waitTask("completed")
/// .timeout(const Duration(seconds: 1), onTimeout: () => "timeout");
/// print(result); // Prints "timeout" after 1 second.
///
/// result = await waitTask("first").timeout(const Duration(seconds: 2),
/// onTimeout: () => waitTask("second"));
/// print(result); // Prints "second" after 7 seconds.
///
/// try {
/// await waitTask("completed").timeout(const Duration(seconds: 2));
/// } on TimeoutException {
/// print("throws"); // Prints "throws" after 2 seconds.
/// }
///
/// var printFuture = waitPrint();
/// await printFuture.timeout(const Duration(seconds: 2), onTimeout: () {
/// print("timeout"); // Prints "timeout" after 2 seconds.
/// });
/// await printFuture; // Prints "printed" after additional 3 seconds.
///
/// try {
/// await waitThrow("error").timeout(const Duration(seconds: 2));
/// } on TimeoutException {
/// print("throws"); // Prints "throws" after 2 seconds.
/// }
/// // StateError is ignored
/// }
///
/// /// Returns [string] after five seconds.
/// Future<String> waitTask(String string) async {
/// await Future.delayed(const Duration(seconds: 5));
/// return string;
/// }
///
/// /// Prints "printed" after five seconds.
/// Future<void> waitPrint() async {
/// await Future.delayed(const Duration(seconds: 5));
/// print("printed");
/// }
/// /// Throws a [StateError] with [message] after five seconds.
/// Future<void> waitThrow(String message) async {
/// await Future.delayed(const Duration(seconds: 5));
/// throw Exception(message);
/// }
/// ```
Future<T> timeout(Duration timeLimit, {FutureOr<T> onTimeout()?});
}
/// Explicitly ignores a future.
///
/// Not all futures need to be awaited.
/// The Dart linter has an optional
/// ["unawaited futures" lint](https://dart.dev/lints/unawaited_futures)
/// which enforces that potential futures
/// (expressions with a static type of [Future] or `Future?`)
/// in asynchronous functions are handled *somehow*.
/// If a particular future value doesn't need to be awaited,
/// you can call `unawaited(...)` with it, which will avoid the lint,
/// simply because the expression no longer has type [Future].
/// Using `unawaited` has no other effect.
/// You should use `unawaited` to convey the *intention* of
/// deliberately not waiting for the future.
///