问题
I see that D has futures, and can create threads but is there anything like "Dart" futures/promises (or I guess javascript if you use a library)?
I want to be able to write code like this -
//
// NOT D code because I can't remember all the syntax!
//
auto fut = myfile.read();
fut.then(function(data) {
// Process data asynchronously when the future completes
};
I want the callback on the same thread as the main code though. This will presumably need some kind of even queue and event dispatching. I think that ASIO in boost provides some similar facilities for C++ but something with a better syntax would be nice.
Is there anything like this in core D, or any simple way to achieve single threaded futures for async code?
回答1:
I suggest you take a look at vibe.d: http://vibed.org/ It enables you to write performant event driven code like you know from nodejs but in a synchronous manner using D's fibers: http://dlang.org/phobos/core_thread.html#.Fiber
回答2:
The std.parallelism library can be used to simulate futures and promises. I have not tried it yet, but it is documented on the page for the library as well as an article discussing concurrency in D. It looks to be pretty easy even if it doesn't explicitly use the words "future" and "promise".
来源:https://stackoverflow.com/questions/21528393/single-threaded-future-promises-in-d