Skip to content

Frees0u1/async

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Async

Write golang code in async/await way

Example

For most production hydration usage, we need fire fetch request asynchronously, and gathering all the response back, and then hydrated all them to a final response. Future will helps such task a lot!

ctx := context.Background()
id := 1
nameFuture := future.NewFuture(func() (GetNameResponse, error) {
    return GetName(GetNameRequest{Id: id})
})
ageFuture := future.NewFuture(func() (GetAgeResponse, error) {
    return GetAge(GetAgeRequest{Id: id})
})
genderFuture := future.NewFuture(func() (GetGenderResponse, error) {
    return GetGender(GetGenderRequest{Id: id})
})

timeout := 80 * time.Millisecond
nameResp, _ := nameFuture.Await(ctx, &timeout)
ageResp, _ := ageFuture.Await(ctx, &timeout)
genderResp, _ := genderFuture.Await(ctx, &timeout)

profile := UserProfile{
    Id:     id,
    Name:   nameResp.Name,
    Age:    ageResp.Age,
    Gender: genderResp.Gender,
}

About

write golang code by async way

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages