elapsed 1.2.1 copy "elapsed: ^1.2.1" to clipboard
elapsed: ^1.2.1 copied to clipboard

Get time elapsed for asynchronous function in a single line of code.

example/README.md

Get time elapsed for asynchronous function in a single line of code.

Pub Version Testing Status


Examples #

Pay attention to the elapsed(...) method calls below. That's the core of this library.

Basic #

  • The getSum(...) function mocks Future with a delay of 2.5s

  • The result of future is accessible in .result property. In this case, it's the sum of two numbers.

  • This example uses .inSeconds time property. Just as the name suggests, it represents the time elapsed in seconds.

      import 'package:elapsed/elapsed.dart';
    
      void main() async {
        var data = await elapsed(getSum(3, 8));
        print(data.result); // prints "11" .
        print('${data.inSeconds} seconds'); // prints "2.5 seconds" .
      }
    
      Future<int> getSum(int a, int b) async {
        await Future.delayed(Duration(milliseconds: 2500));
        return a + b;
      }
    

Future<void> #

  • Both futures with return types and plain void is supported by this library.

  • This time the Future is mocked with 3 seconds delay.

      import 'package:elapsed/elapsed.dart';
    
      void main() async {
        var data = await elapsed(divide(10, 2));
        print('${data.inSeconds} seconds'); // prints "3.0 seconds" .
      }
    
      Future<void> divide(int a, int b) async {
        await Future.delayed(Duration(milliseconds: 3000));
        print(a / b); // prints "5.0"
        // no return value...
      }
    

HTTP Request->Response #

  • Of course, this works too!

  • This example will actually call an HTTP api resource.

  • Using jsonplaceholder.

  • The code wraps it inside elapsed(...) instead of directly awaiting http.get(...).

      import 'package:elapsed/elapsed.dart';
      import 'package:http/http.dart' as http;
    
      void main() async {
        var response = await getSampleREST('https://jsonplaceholder.typicode.com/posts/1');
        print(response.body); // prints JSON data from the api.
      }
    
      Future<http.Response> getSampleREST(String url) async {
        var data = await elapsed(http.get(url)); // HTTP REST example.
        print('${data.inSeconds} seconds'); // seconds printed depends on internet speed.
        print(data.result.statusCode); // prints "200" .
        return data.result;
      }
    
5
likes
140
pub points
38%
popularity

Publisher

verified publisherxamantra.dev

Get time elapsed for asynchronous function in a single line of code.

Repository (GitHub)
View/report issues

Documentation

API reference

License

BSD-3-Clause (LICENSE)

More

Packages that depend on elapsed