New create a new promise instance.
(runnable func(resolve func(T), reject func(error)))
| 28 | |
| 29 | // New create a new promise instance. |
| 30 | func New[T any](runnable func(resolve func(T), reject func(error))) *Promise[T] { |
| 31 | if runnable == nil { |
| 32 | panic("runnable function should not be nil") |
| 33 | } |
| 34 | |
| 35 | p := &Promise[T]{ |
| 36 | runnable: runnable, |
| 37 | pending: true, |
| 38 | mu: &sync.Mutex{}, |
| 39 | wg: &sync.WaitGroup{}, |
| 40 | } |
| 41 | |
| 42 | defer p.run() |
| 43 | |
| 44 | return p |
| 45 | } |
| 46 | |
| 47 | func (p *Promise[T]) run() { |
| 48 | p.wg.Add(1) |