Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Await future for a specific time

How would you wait for future response for a specific amount of time?

Say, we make a http post request and await for its response before we close the http request, but, we wait for only 3 secs, else we close the request.

How would you achieve that?

Something like

Future makePostReq() async{
  .... 

  await http response for 3 secs

  .... 

 if(response) {
  ... Do something with it
 }

 Http.close

} 
like image 235
Pushan Gupta Avatar asked Oct 05 '18 19:10

Pushan Gupta


People also ask

What is asynchronous Future?

When you call an asynchronous function, it returns an uncompleted future. That future is waiting for the function's asynchronous operation to finish or to throw an error.

How do you write await?

Await Syntax The keyword await before a function makes the function wait for a promise: let value = await promise; The await keyword can only be used inside an async function.

How do you use await properly?

Inside an async function, you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.

Is Dart synchronous or asynchronous?

Dart uses Future objects to represent asynchronous operations.

How do you wait for a future to complete?

Now that you have an asyncfunction, you can use the awaitkeyword to wait for a future to complete: print(awaitcreateOrderMessage()); As the following two examples show, the asyncand awaitkeywords result in asynchronous code that looks a lot like synchronous code.

How do I wait for a future to complete with async?

Now that you have an asyncfunction, you can use the awaitkeyword to wait for a future to complete: print(awaitcreateOrderMessage());

What is the future of a function waiting for?

That future is waiting for the function’s asynchronous operation to finish or to throw an error. Completed If the asynchronous operation succeeds, the future completes with a value.

What is a future?

What is a future? A future (lower case “f”) is an instance of the Future(capitalized “F”) class. A future represents the result of an asynchronous operation, and can have two states: uncompleted or completed. Note:Uncompletedis a Dart term referring to the state of a future before it has produced a value.


Video Answer


3 Answers

You can use Future.any constructor to make a race condition

final result = await Future.any([
  Future.value(42),
  Future.delayed(const Duration(seconds: 3))
]);

You can also use Future.timeout method

final result = await Future.value(42).timeout(const Duration(seconds: 3));
like image 180
Rémi Rousselet Avatar answered Oct 12 '22 03:10

Rémi Rousselet


You can do it very easily

try {
       var response = await Http.get("YourUrl").timeout(const Duration(seconds: 3));
       if(response.statusCode == 200){
          print("Success");
       }else{
          print("Something wrong");
       }
 } on TimeoutException catch (e) {
     print('Timeout');
 } on Error catch (e) {
     print('Error: $e');
 }

This example sets timeout to 3 second. If it has been 3 seconds and no response received, it will throw TimeoutException

Import this :

import 'package:http/http.dart' as Http;
import 'dart:async';
like image 30
Sanjayrajsinh Avatar answered Oct 12 '22 03:10

Sanjayrajsinh


Future.any([asyncfunc, ...])

Here's an example of using Remi's Future.any solution where the future that returns first, will be used. The other is discarded.

So, the first future is your data-gathering/slow function and the other is a fallback when your call is taking too long.

    dynamic result = await Future.any([
      getData(fakeDelay: seconds), // ← hope this returns first
      timeoutAfter(sec: timeout, onTimeout: () => 'Timed Out!', ) // ← waited too long, do this
    ]);

Example in Flutter Page

Here's a copy/paste example for a Flutter page:

(look at your debug/run output window for messages)

import 'package:flutter/material.dart';

class FutureTimeoutPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Future or Timeout Page'),
      ),
      body: FutureAnyExample(),
    );
  }
}

class FutureAnyExample extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisAlignment: MainAxisAlignment.center,
      children: [
        Text('Complete before timeout or timeout:'),
        SizedBox(height: 30,),
        Row(
          mainAxisAlignment: MainAxisAlignment.spaceEvenly,
          children: [
            ElevatedButton(onPressed: () => getDataOrTimeout(seconds: 1, timeout: 3),
                child: Text('In Time')),
            ElevatedButton(onPressed: () => getDataOrTimeout(seconds: 5, timeout: 3),
                child: Text('Too Slow'))
          ],
        )
      ],
    );
  }

  Future<void> getDataOrTimeout({int seconds, int timeout}) async {
    /// In Future.any, put as many async functions as you need.
    /// Whichever completes first, will be returned. All others are discarded
    dynamic result = await Future.any([
      getData(fakeDelay: seconds), // ← hope this returns first
      timeoutAfter(sec: timeout, onTimeout: () => 'Timed Out!', ) // ← waited too long, do this
    ]);

    print(result);
  }

  /// Mock of a long-running operation like getting DB data, or API call
  Future<String> getData({int fakeDelay}) async {
    return Future.delayed(Duration(seconds: fakeDelay), () => 'Data returned!');
  }

  /// Do this in case my long-running op takes too long
  /// Can run a function or just return some message
  Future<dynamic> timeoutAfter({int sec, Function() onTimeout}) async {
    return Future.delayed(Duration(seconds: sec), onTimeout);
  }
}
like image 5
Baker Avatar answered Oct 12 '22 03:10

Baker