blob: c4247594577f6c539a0886dcad063aa58f737d04 [file] [log] [blame]
Jon Perrittfa2c65e2014-10-02 20:32:43 -05001package gophercloud
2
3import (
4 "fmt"
5 "time"
6)
7
8// WaitFor polls a predicate function once per second up to secs times to wait for a certain state to arrive.
9func WaitFor(secs int, predicate func() (bool, error)) error {
10 for i := 0; i < secs; i++ {
11 time.Sleep(1 * time.Second)
12
13 satisfied, err := predicate()
14 if err != nil {
15 return err
16 }
17 if satisfied {
18 return nil
19 }
20 }
21 return fmt.Errorf("Time out in WaitFor.")
22}