Created
January 29, 2020 12:52
-
-
Save ChangJoo-Park/37a4e18ffc0f09161d74327230533457 to your computer and use it in GitHub Desktop.
make future timer in dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'dart:async'; | |
void main() async { | |
for (int i = 0; i < 5; i++) { | |
String result = await makeFutureTimer( | |
duration: Duration(seconds: 0), | |
action: () { | |
print(DateTime.now()); | |
return 'RETURN VALUE $i'; | |
}, | |
); | |
print(result); | |
} | |
List<Future> requests = []; | |
for (int j = 0; j < 5; j++) { | |
requests.add(makeFutureTimer( | |
duration: Duration(seconds: 1), | |
action: () { | |
print(DateTime.now()); | |
return 'RETURN VALUE $j'; | |
}, | |
),); | |
} | |
var multipleResult = await Future.wait(requests) ; | |
print(multipleResult); | |
} | |
Future makeFutureTimer({Duration duration, Function action}) { | |
final Completer completer = Completer(); | |
Timer(duration, () { | |
String returnValue = action(); | |
completer.complete(returnValue); | |
}); | |
return completer.future; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment