From 418f3beebbe9ac34e46ac88aae63180500df5d0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=C4=9Fuzhan=20Kazak?= Date: Sun, 30 Apr 2023 14:09:10 +0300 Subject: [PATCH] Added "future_promise.md" file with an explanation of the future/promise pattern as reference. --- messaging/future_promise.md | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 messaging/future_promise.md diff --git a/messaging/future_promise.md b/messaging/future_promise.md new file mode 100644 index 0000000..767f8aa --- /dev/null +++ b/messaging/future_promise.md @@ -0,0 +1,50 @@ +# Future & Promise Pattern + + Future pattern (also called Promise pattern) is a design pattern used in concurrent and asynchronous programming, which allows clients to start a potentially long-running operation and obtain its result in a non-blocking way by returning a placeholder object (a future or a promise). + + When the operation completes, the result is set on the future/promise object, which can then be obtained by the client code through a blocking or non-blocking method call. + +### Types + +```go +package main + +import ( + "fmt" + "time" +) + +type FutureInt struct { + ch chan int +} +``` + +### Implementation + +```go +func (f *FutureInt) Get() int { + return <-f.ch +} + +func NewFutureInt() *FutureInt { + return &FutureInt{ + ch: make(chan int), + } +} + +func add(a, b int) *FutureInt { + f := NewFutureInt() + go func() { + time.Sleep(3 * time.Second) // Simulate long running task + f.ch <- a + b + }() + return f +} + +func main() { + fmt.Println("Starting task...") + f := add(2, 3) + fmt.Println("Task started...") + fmt.Printf("Result: %d\n", f.Get()) +} +```