Thursday, November 15, 2012

ALE Event Loop Framework - Added Promises

Did a little work tonight on ALE, my event loop framework for .NET. Decided to add a Promise implementation that leveraged the event loop. I fiddled with the usage a bit, so hopefully it's to people's liking.  I'm looking for any and all feedback on this addition.

For those of you that aren't familiar with promises, I did a blog entry a while ago about promises in JavaScript. This is pretty much the same idea. While in .NET there are WaitHandles and Tasks for synchronizing multiple asynchronous calls, I wanted something that would leverage the ALE's event loop engine that would do the same thing. So I decided to write a simple promise implementation.

Basic usage looks like this:

//create a single promise.
Promise.To((defer) => 
    try {
       //Do something here, and return a value
       defer.Reject("this has been rejected.");
}).Then((data) => 
    //When the above is complete, write the return value

More advanced usage would be like so:

//a method to create a promise and run it.
public Promise Foo(string bar) {
    return Promise.To((defer) => 

//using When to await the completion of multiple Promises.
Promise.When(Foo("One"), Foo("Two"), Foo("Three"))
   .Then((data) =>

The above code would be used in situations where you might need to wait for multiple asynchronous calls before continuing. Using ALE's Promise class, you'll be able to do so without blocking a thread.

I'm really looking for any feedback/code-review anyone wants to do. So if you have the time please look it over.

Upcoming for ALE, I'm going to try to make most of the Asynchronous calls return a Promise, to enable more complex chaining of async calls. Also, possibly a method to re-execute a Promise.

No comments:

Post a Comment

This form allows some basic HTML. It will only create links if you wrap the URL in an anchor tag (Sorry, it's the Blogger default)