1 /* 2 * Hunt - A refined core library for D programming language. 3 * 4 * Copyright (C) 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 <C> the type of the context object 20 */ 21 interface Promise(C) { 22 23 string id(); 24 25 /** 26 * <p>Callback invoked when the operation completes.</p> 27 * 28 * @param result the context 29 * @see #failed(Throwable) 30 */ 31 void succeeded(C result); 32 33 /** 34 * <p>Callback invoked when the operation fails.</p> 35 * 36 * @param x the reason for the operation failure 37 */ 38 void failed(Exception x) ; 39 40 /** 41 * <p>Empty implementation of {@link Promise}.</p> 42 * 43 * @param (U) the type of the result 44 */ 45 class Adapter(U) : Promise!U { 46 47 void succeeded(C result){ 48 49 } 50 51 void failed(Throwable x) { 52 53 } 54 } 55 } 56 57 58 class DefaultPromise(C) : Promise!C 59 { 60 string id() { return "default"; } 61 62 void succeeded(C result) { 63 } 64 65 void failed(Exception x) { 66 } 67 } 68