Discussion:
[dart-misc] Problem with asynchronous programming
Mateusz Lewandowski
2016-08-12 07:38:43 UTC
Permalink
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?
--
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+***@dartlang.org.
'Lasse R.H. Nielsen' via Dart Misc
2016-08-12 09:16:47 UTC
Permalink
On Fri, Aug 12, 2016 at 9:38 AM, Mateusz Lewandowski <
Post by Mateusz Lewandowski
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.
(
https://api.dartlang.org/stable/1.16.1/dart-async/StreamSubscription/asFuture.html
)
Post by Mateusz Lewandowski
}
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?
Post by Mateusz Lewandowski
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 - ***@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
--
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+***@dartlang.org.
Mateusz Lewandowski
2016-08-12 10:05:35 UTC
Permalink
Thanks a lot.
I will try it today and give reponse if works :)

2016-08-12 11:16 GMT+02:00 'Lasse R.H. Nielsen' via Dart Misc <
Post by 'Lasse R.H. Nielsen' via Dart Misc
On Fri, Aug 12, 2016 at 9:38 AM, Mateusz Lewandowski <
Post by Mateusz Lewandowski
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.
var c = new Completer();
req.listen((_) {
jsonData = req.responseText;
c.complete();
});
await c.future;
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.
(https://api.dartlang.org/stable/1.16.1/dart-async/
StreamSubscription/asFuture.html)
Post by Mateusz Lewandowski
}
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?
Post by Mateusz Lewandowski
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.
var c = new Completer<String>();
req.listen((_) {
c.complete(req.responseText);
});
return c.future;
String response = await.context.loadData();
/L
--
'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
--
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
--
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+***@dartlang.org.
Mateusz Lewandowski
2016-08-15 09:35:15 UTC
Permalink
It works :)
Post by Mateusz Lewandowski
Thanks a lot.
I will try it today and give reponse if works :)
2016-08-12 11:16 GMT+02:00 'Lasse R.H. Nielsen' via Dart Misc <
Post by 'Lasse R.H. Nielsen' via Dart Misc
On Fri, Aug 12, 2016 at 9:38 AM, Mateusz Lewandowski <
Post by Mateusz Lewandowski
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.
var c = new Completer();
req.listen((_) {
jsonData = req.responseText;
c.complete();
});
await c.future;
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.
(https://api.dartlang.org/stable/1.16.1/dart-async/StreamSub
scription/asFuture.html)
Post by Mateusz Lewandowski
}
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?
Post by Mateusz Lewandowski
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.
var c = new Completer<String>();
req.listen((_) {
c.complete(req.responseText);
});
return c.future;
String response = await.context.loadData();
/L
--
'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
--
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
--
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+***@dartlang.org.
Loading...