Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise Branching #19

Merged
merged 3 commits into from
May 6, 2020
Merged

Promise Branching #19

merged 3 commits into from
May 6, 2020

Conversation

Mike-Dax
Copy link
Contributor

@Mike-Dax Mike-Dax commented May 5, 2020

Closes #18

Every method except Await now returns a new Promise to allow for branching logic.

This is a breaking API change.

Where previously this would await the "Caught and Then'd" version of promise, it now branches into the caught branch and the "Then'd" branch.


	var promise = New(func(resolve func(interface{}), reject func(error)) {
		reject(errors.New("very serious err"))
	})

	promise.
		Catch(func(err error) error {
			if err.Error() == "very serious err" {
				return errors.New("dealing with err at this stage")
			}
			return nil
		}).
		Catch(func(err error) error {
			if err.Error() != "dealing with err at this stage" {
				t.Error("Error doesn't propagate")
			}
			return nil
		})
	
	promise.Then(func(data interface{}) interface{} {
		t.Error("Then triggered in .Catch test")
		return nil
	})

	promise.Await()

The functionality now is more similar to how the JS API works.

This would be the idiomatic way to write the above. The promise variable is the caught and "Then'd" promise.


	var promise = New(func(resolve func(interface{}), reject func(error)) {
		reject(errors.New("very serious err"))
	}).Catch(func(err error) error {
			if err.Error() == "very serious err" {
				return errors.New("dealing with err at this stage")
			}
			return nil
		}).
		Catch(func(err error) error {
			if err.Error() != "dealing with err at this stage" {
				t.Error("Error doesn't propagate")
			}
			return nil
		}).Then(func(data interface{}) interface{} {
		t.Error("Then triggered in .Catch test")
		return nil
	})

	promise.Await()

One of the tests was updated to reflect this, I didn't touch the other tests.

Allocations are obviously worse now that this copies all the time.

BenchmarkNew-12           	 3395116	       356 ns/op	     128 B/op	       3 allocs/op
BenchmarkThen-12          	  537918	      2036 ns/op	     568 B/op	      16 allocs/op
BenchmarkCatch-12         	  636610	      1836 ns/op	     576 B/op	      16 allocs/op
BenchmarkAwait-12         	  140638	      8473 ns/op	    3456 B/op	      99 allocs/op
BenchmarkResolve-12       	  462471	      2718 ns/op	     456 B/op	      13 allocs/op
BenchmarkReject-12        	  448644	      2656 ns/op	     464 B/op	      13 allocs/op
BenchmarkAll-12           	   84603	     15931 ns/op	    5905 B/op	     150 allocs/op
BenchmarkAllSettled-12    	   71997	     15515 ns/op	    5842 B/op	     149 allocs/op
BenchmarkRace-12          	  304389	      3860 ns/op	    1408 B/op	      37 allocs/op

@chebyrash
Copy link
Owner

chebyrash commented May 6, 2020

@Mike-Dax thank you for good work
Looks good to me, merging this.

@chebyrash chebyrash merged commit 66b4c53 into chebyrash:master May 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Promise Branching
2 participants