Skip to content

Commit

Permalink
feat(coroutine): Added Wait (#85)
Browse files Browse the repository at this point in the history
Signed-off-by: Flc゛ <[email protected]>
  • Loading branch information
flc1125 authored Jan 31, 2024
1 parent eabf8d5 commit 4c8992a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
13 changes: 13 additions & 0 deletions coroutine/wait.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package coroutine

import "sync"

func Wait(fn func()) {
var wg sync.WaitGroup
wg.Add(1)
go func() {
fn()
wg.Done()
}()
wg.Wait()
}
19 changes: 19 additions & 0 deletions coroutine/wait_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package coroutine

import (
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestWait(t *testing.T) {
var msg string

Wait(func() {
time.Sleep(time.Second)
msg = "hello"
})

assert.Equal(t, "hello", msg)
}

0 comments on commit 4c8992a

Please sign in to comment.