-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
266775c
commit f680957
Showing
5 changed files
with
160 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package async_test | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/ianlopshire/go-async" | ||
) | ||
|
||
type Container struct { | ||
async.Future | ||
Value interface{} | ||
Err error | ||
} | ||
|
||
func NewContainer() (*Container, func(interface{}, error)) { | ||
fut, resolve := async.NewFuture() | ||
container := &Container{ | ||
Future: fut, | ||
} | ||
|
||
fn := func(value interface{}, err error) { | ||
container.Value = value | ||
container.Err = err | ||
resolve() | ||
} | ||
|
||
return container, fn | ||
} | ||
|
||
// This example demonstrates how a Future can be embedded into a container. | ||
func ExampleFuture_container() { | ||
// Define a value that will be available in the future. | ||
container, resolve := NewContainer() | ||
|
||
// Simulate long computation or IO by sleeping before and resolving the future. | ||
go func() { | ||
time.Sleep(500 * time.Millisecond) | ||
resolve("Hello World!", nil) | ||
}() | ||
|
||
// Block until the future is resolved. | ||
async.Await(container) | ||
|
||
fmt.Println(container.Value, container.Err) | ||
// output: Hello World! <nil> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package async_test | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/ianlopshire/go-async" | ||
) | ||
|
||
func ExampleFuture() { | ||
// Define a value that will be available in the future. | ||
var val string | ||
fut, resolve := async.NewFuture() | ||
|
||
// Simulate long computation or IO by sleeping before setting the value and resolving | ||
// the future. | ||
go func() { | ||
time.Sleep(500 * time.Millisecond) | ||
val = "Hello World!" | ||
resolve() | ||
}() | ||
|
||
// Block until the future is resolved. | ||
async.Await(fut) | ||
|
||
fmt.Println(val) | ||
// output: Hello World! | ||
} | ||
|
||
func ExampleFuture_select() { | ||
// Define a value that will be available in the future. | ||
var val string | ||
fut, resolve := async.NewFuture() | ||
|
||
// The channel returned by Done() can be used directly in a select statement. | ||
select { | ||
case <-fut.Done(): | ||
fmt.Println(val) | ||
default: | ||
fmt.Println("Future not yet resolved") | ||
} | ||
|
||
// Simulate long computation or IO by sleeping before setting the value and resolving | ||
// the future. | ||
go func() { | ||
time.Sleep(500 * time.Millisecond) | ||
val = "Hello World!" | ||
resolve() | ||
}() | ||
|
||
// Block until the future is resolved. | ||
<-fut.Done() | ||
|
||
fmt.Println(val) | ||
// output: | ||
// Future not yet resolved | ||
// Hello World! | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters