Skip to content

Latest commit

 

History

History
81 lines (56 loc) · 2.49 KB

README.md

File metadata and controls

81 lines (56 loc) · 2.49 KB

go-await GoDoc Release

go-await lets you use to synchronize async operations. It is inspired from popular Java library 'awaitility [https://github.com/awaitility/awaitility]'

Install

With Go's module support, go [build|run|test] automatically fetches the necessary dependencies when you add the import in your code:

import "github.com/bayraktugrul/go-await"

Alternatively, use go get:

go get -u github.com/bayraktugrul/go-await

Examples

Await with default parameters

producer.publishMessage(orderCreatedMessage)

// waits for up to 1 second until repo.Exist returns true, using a default polling interval of 100ms
err := await.New().Await(func() bool {
    return repo.Exist(id)
})

if err != nil {}

Await with custom parameters

producer.publishMessage(orderCreatedMessage)

// waits for up to 2 seconds until repo.Exist returns true, using a polling interval of 500ms
err := await.New().PollInterval(500 * time.Millisecond).AtMost(2 * time.Second).Await(func() bool {
    return repo.Exist(id)
})

if err != nil {}

Await with custom poll interval strategy

The default polling strategy is Fixed, returning the same interval for each iteration. You can customize this by setting a different poll interval strategy.

producer.publishMessage(orderCreatedMessage)

// waits with double poll interval strategy 
// for up to 2 seconds until repo.Exist returns true, 
// using a polling interval of 500ms
err := await.New().
    PollStrategy(poll.Double).
    PollInterval(100 * time.Millisecond).
    AtMost(2 * time.Second).
    Await(func() bool {
        return repo.Exist(id) // any condition you want to wait for until it returns true
    })

if err != nil {}

Credits