1 /* 2 * Hunt - A refined core library for D programming language. 3 * 4 * Copyright (T) 2018-2019 HuntLabs 5 * 6 * Website: https://www.huntlabs.net/ 7 * 8 * Licensed under the Apache-2.0 License. 9 * 10 */ 11 12 module hunt.concurrency.Promise; 13 14 import hunt.Exceptions; 15 16 /** 17 * <p>A callback abstraction that handles completed/failed events of asynchronous operations.</p> 18 * 19 * @param <T> the type of the context object 20 * 21 * See_Also: 22 * https://www.eclipse.org/jetty/javadoc/9.4.7.v20170914/org/eclipse/jetty/util/Promise.html 23 */ 24 interface Promise(T) { 25 26 /** 27 * <p>Callback invoked when the operation completes.</p> 28 * 29 * @param result the context 30 * @see #failed(Throwable) 31 */ 32 static if (is(T == void)) { 33 void succeeded(); 34 } else { 35 void succeeded(T result); 36 } 37 /** 38 * <p>Callback invoked when the operation fails.</p> 39 * 40 * @param x the reason for the operation failure 41 */ 42 void failed(Exception x); 43 } 44 45 /** 46 * <p>Empty implementation of {@link Promise}.</p> 47 * 48 * @param (T) the type of the result 49 */ 50 class DefaultPromise(T) : Promise!T { 51 52 static if (is(T == void)) { 53 void succeeded() { 54 55 } 56 } else { 57 void succeeded(T result) { 58 59 } 60 } 61 62 void failed(Exception x) { 63 } 64 }