Problem with asynchronous programming

71 views
Skip to first unread message

Mateusz Lewandowski

unread,
Aug 12, 2016, 3:38:44 AM8/12/16
to Dart Misc
Hi
Like in topic i hava a problem with Future method in ExchangeData Class
class looks following (it is only templeta becouse when i was writing this post i had not my code)

string jsonData;
class ExchangeData
{

Future LoadData() async
{
HttpRequest req= new

...

req.send(...)

req.listen
(

jsonData=req.ResponseText;

)
}

String GetJson()
{
return this.jsonData;
}
}

I am trying to use it in
main() async
{
   ExchangeData context=new ExchangeData();
 await context.LoadData();
String response=context.GetJson();

}

The problem is that await does not waiting for data load and program execution go to next instruction.
When i use LoadData like a function not method from class, it works.
How to wait for response from class method LoadData?
Maybe is there better way to return data from Future function?

Lasse R.H. Nielsen

unread,
Aug 12, 2016, 5:17:13 AM8/12/16
to mi...@dartlang.org
On Fri, Aug 12, 2016 at 9:38 AM, Mateusz Lewandowski <matmat01...@gmail.com> wrote:
Hi
Like in topic i hava a problem with Future method in ExchangeData Class
class looks following (it is only templeta becouse when i was writing this post i had not my code)

string jsonData;
class ExchangeData
{

Future LoadData() async
{
HttpRequest req= new

...

req.send(...)

req.listen
(

jsonData=req.ResponseText;

)

As you say, the problem is that you don't wait until the data is available.
To do that, you must create a future that completes when the data is there, so you have something to wait on.

One solution is to end the function with:

var c = new Completer();
req.listen((_) { 
  jsonData = req.responseText; 
  c.complete();
});
await c.future;

Another option is to just do:

var subscription = req.listen(...);
await subscription.asFuture();

because the `asFuture` exactly returns a future that completes when the stream is done, but it doesn't affect the onData listener.

  
 
}

String GetJson()
{
return this.jsonData;
}
}

I am trying to use it in
main() async
{
   ExchangeData context=new ExchangeData();
 await context.LoadData();
String response=context.GetJson();

}

The problem is that await does not waiting for data load and program execution go to next instruction.
When i use LoadData like a function not method from class, it works.

That sounds curious. Are you sure it's exactly the same function?
 
How to wait for response from class method LoadData?
Maybe is there better way to return data from Future function?

Absolutely. Just return the data as the value of the future.
In this case, that would just be:

   var c = new Completer<String>();
   req.listen((_) {
     c.complete(req.responseText);
   });
   return c.future;

Then you can just do:

String response = await.context.loadData();

/L
--
Lasse R.H. Nielsen - l...@google.com  
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K - Denmark - CVR nr. 28 86 69 84

Mateusz Lewandowski

unread,
Aug 12, 2016, 6:05:40 AM8/12/16
to mi...@dartlang.org
Thanks a lot.
I will try it today and give reponse if works :)

--
For other discussions, see https://groups.google.com/a/dartlang.org/
 
For HOWTO questions, visit http://stackoverflow.com/tags/dart
 
To file a bug report or feature request, go to http://www.dartbug.com/new
---
You received this message because you are subscribed to the Google Groups "Dart Misc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.

Mateusz Lewandowski

unread,
Aug 15, 2016, 5:35:20 AM8/15/16
to mi...@dartlang.org
It works :)
Reply all
Reply to author
Forward
0 new messages