# HG changeset patch # User cin # Date 1416095352 -10800 # Node ID 1a8426e6e895a6f625001fc48706581e31fcd1a0 # Parent 1b7ebcc52e5a43ffde8efae07336f938b9f3cefe added promise timeout helper diff -r 1b7ebcc52e5a -r 1a8426e6e895 Implab/PromiseExtensions.cs --- a/Implab/PromiseExtensions.cs Fri Nov 14 14:04:24 2014 +0300 +++ b/Implab/PromiseExtensions.cs Sun Nov 16 02:49:12 2014 +0300 @@ -72,6 +72,23 @@ } }; } + + static void CancelCallback(object cookie) { + ((ICancellable)cookie).Cancel(); + } + + /// + /// Cancells promise after the specified timeout is elapsed. + /// + /// The promise to cancel on timeout. + /// The timeout in milliseconds. + /// The 1st type parameter. + public static TPromise Timeout(this TPromise that, int milliseconds) where TPromise : IPromise { + Safe.ArgumentNotNull(that, "that"); + var timer = new Timer(CancelCallback, that, milliseconds, -1); + that.On(timer.Dispose, PromiseEventType.All); + return that; + } #if NET_4_5