Skip to content

Enhancement Request: Add '.toFuture()' or '.toAsync()' as default method for all Function types #2705

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
DartBot opened this issue Apr 23, 2012 · 5 comments
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. type-enhancement A request for a change that isn't a bug
Milestone

Comments

@DartBot
Copy link

DartBot commented Apr 23, 2012

This issue was originally filed by prujo...@gmail.com


I find myself writing a function-to-future wrapper in most of my non-trivial projects.

Putting forward for consideration that it makes sense to just add a standard conversion to Future method on all Function types.

Thanks,
John

@DartBot
Copy link
Author

DartBot commented Apr 24, 2012

This comment was originally written by jat@google.com


And what would the implementation look like? How would it convert the synchronous body of the function into an async one?

@madsager
Copy link
Contributor

Removed Type-Defect label.
Added Type-Enhancement, Area-Language, Triaged labels.

@DartBot
Copy link
Author

DartBot commented Apr 24, 2012

This comment was originally written by prujoh...@gmail.com


@JaT I'm just a simple caveman, but this is how I approach it:

  Future f2f(Function f){
    Completer c = new Completer();
    
    doIt() {
      c.complete(f());
    }
    
    try{
      window.setTimeout(doIt, 0);
    }catch (Exception e){
      c.completeException(e);
    }
    
    return c.future;
  }
  
  String expensiveFunc(int ms){
    var time = new Date.now();
    var nextTime = time;
    while(nextTime.difference(time).inMilliseconds < ms){
      nextTime = new Date.now();
    }
    return "Expensive function finished.";
  }
  
  f2f(() => expensiveFunc(5000)).then((v) => print(v));
  
  print('I print first.');

  // instead it would be nice if f2f() was standard method of Function...
  // expensiveFunc(5000).toFuture().then((v) => print(v));
  //
  // also perhaps option to direct to a thread pool (wrapped in a Isolate [constrained that func must return isolate-legal value])
  // expensiveFunc(5000).toIsolate().then((v) => print(v));

@gbracha
Copy link
Contributor

gbracha commented Apr 29, 2012

Removed Area-Language label.
Added Area-Library label.

@sethladd
Copy link
Contributor

sethladd commented Apr 8, 2013

Thanks for the suggestion! Implemented with Future.of()


Added this to the M4 milestone.
Added Fixed label.

@DartBot DartBot added Type-Enhancement area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Apr 8, 2013
@DartBot DartBot added this to the M4 milestone Apr 8, 2013
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed type-enhancement labels Mar 1, 2016
This issue was closed.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
area-core-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants