Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dart wait for inner Future

Tags:

mysql

dart

I'm trying to execute some queries which have a reference as foreign keys on each other, so I have to wait until the outer future is finished. This algorithm is placed inside a function which returns a future.

Future<List<int>> newEntry(Session session) {
    bool success = true;
    List<int> error = Helpers.formatJsonAndEncodeUtf8(
    "ERROR 3623231 (Experiencebox could not be saved )", session: session);
    Future coucheFutureQuerySave = new Future(() {
      new Couchbase().couchConnExperiencebox.set(uniqueId, UTF8.encode(
          documentOriented.toString())).catchError((error) {
        print("COUCHEBASE ERROR 338420: " + error.toString());
        success = false;
      });
    }).catchError((error) {
      print("COUCHEBASE ERROR 338121: " + error.toString());
      success = false;
    });
    // Save data to MySql Database
    //create archiv entry
    Future <Results> mysql = mysqlCon.query(
        getQuery()).catchError((error) {
      print("failed archive");
      success = false;
    }).whenComplete((){
      print("completed archive");
      //querystring for insert word
      String wordQuery = new Tag().getInsertWordIfNotExists(tags);
      mysqlCon.query( wordQuery ).catchError((error) {
        print("failed word");
        success = false;
      }).whenComplete((){
        print("completed word");
        //query string for insert tags in blog
        String tagQuery = new Tag().getInsertTagsInBlogIfNotExists(tags, uniqueId);
        mysqlCon.query( tagQuery ).catchError((error) {
          print("failed tag in blog");
          success = false;
        });
      });
    });
    return Future.wait([mysql, coucheFutureQuerySave]).then((e) {
      // Save data to CoucheBase
      if (success) {
        print("completed all");
        return Helpers.formatJsonAndEncodeUtf8('OK', session: session, data:
            [new JsonObject.fromJsonString('{"id":"' + uniqueId + '"}')]);
      } else {
        print("failed");
        success = false;
      }
    });
}

when running it i got this out:

=== TO CLIENT ===
completed archive
completed all
=== TO CLIENT ===
failed word
completed word
completed tag in blog
completed voting

So my question is, how can I wait for Future mysql until it has completed its "childs" (in whenCompleted() function). I also tried with then() instead of whenCompleted() but this doesn't make any differences.

like image 908
fsulser Avatar asked Aug 21 '26 11:08

fsulser


1 Answers

Ensure that you always return the future when you call async functions within async functions to keep them connected. When you call an async function like

.then((x) => someAsync())

the future returned from someAsync() is automatically returned but when your code has a block body like

.then((x) {
  return someAsync()
 })

you need to return explicitly.

There are two lines which start with

mysqlCon.query(

which should actually be

return mysqlCon.query(

This line

new Couchbase()

should probably be

return new Couchbase()

Using the new async/await feature can simplify your code notably.

like image 124
Günter Zöchbauer Avatar answered Aug 24 '26 06:08

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!