/async

A safe way to execute functions asynchronously, recovering them in case of panic. It also provides an error stack aiming to facilitate fail causes discovery.

Primary LanguageGoBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Async

Build Status codecov Go Report Card GoDoc

Provides a safe way to execute fns's functions asynchronously, recovering them in case of panic. It also provides an error stack aiming to facilitate fail causes discovery.

Usage

func InsertAsynchronously(ctx context.Context) error {
	transaction := db.Transaction().Begin()

	err := async.Run(ctx,
		func(_ context.Context) error {
			_, err := transaction.Exec(`
				INSERT INTO foo (bar)
				VALUES ('Hello')
			`)

			return err
		},

		func(_ context.Context) error {
			_, err := transaction.Exec(`
				INSERT INTO foo (bar)
				VALUES ('world')
			`)

			return err
		},

		func(_ context.Context) error {
			_, err := transaction.Exec(`
				INSERT INTO foo (bar)
				VALUES ('asynchronously!')
			`)

			return err
		},
	)

	if err != nil {
		e := transaction.Rollback()
		log.IfError(e)
		return err
	}

	return transaction.Commit()
}