wg vs chan

mail@pastecode.io avatar
unknown
golang
2 years ago
1.1 kB
3
Indexable
Never
func PrepareTariffData(
	api *CorpTariffsHacathon2022API, ctx context.Context,
	driverTariffId string, corpTariffId string, tariffPlanId string) TariffData {

	chanTariffPlan := api.tariffPlansStorage.AsyncGetById(ctx, tariffPlanId)
	chanDriverTariff := api.individualTariffsStorage.AsyncGetById(ctx, driverTariffId)
	chanCorpTariff := api.tariffsStorage.AsyncGetById(ctx, corpTariffId)

	data := TariffData{}
	data.corpTariffPlan = <-chanTariffPlan
	data.driverTariff = <-chanDriverTariff
	data.corpTariff = <-chanCorpTariff

	return data
}

func PrepareTariffData(
	api *CorpTariffsHacathon2022API, ctx context.Context,
	driverTariffId string, corpTariffId string, tariffPlanId string) TariffData {

	var wg sync.WaitGroup

	data := TariffData{}

	go func() {
		data.corpTariffPlan = api.tariffPlansStorage.GetById(ctx, tariffPlanId)
		wg.Done()
	}()

	go func() {
		data.driverTariff = api.individualTariffsStorage.GetById(ctx, driverTariffId)
		wg.Done()
	}()

	go func() {
		data.corpTariff = api.tariffsStorage.GetById(ctx, corpTariffId)
		wg.Done()
	}()

	wg.Done()

	return data
}