gj
gj copied to clipboard
Consider reject/resolve instead of err/ok for Promises
This may just be bikeshedding, I can't really tell.
The first thing that called my attention when looking into this project is the use of err
/ok
naming, resembling (likely intentionally) a lot the namings in Result
. I'd like to argue against it precisely for that reason.
Another reason is that, in JavaScript land at least, resolve
/reject
is already both standard the de facto names.
My opinion is that trying to look like a Result
while the way it works being considerably different is misleading. If there is a way to convert (or extract from) a Promise
to a Result
, it would be better to tell them apart more easily.
What do you think?
Hi!
Yes, the idea with Promise::ok()
and Promise::err()
is to draw a parallel with Result::Ok
and Result::Err
. I'm not sure that I understand your worry about being able to tell these types apart. If someone puts Ok(())
where they should have put Promise::ok(())
, the type system will correct them soon enough.
I don't like that the names resolve
and reject
are verbs. Using verbs like these for plain data constructors strikes me as weird.
My experience in Javascript is that it's actually very rare that you would need to use Promise.resolve(x)
, because you can usually just return x
instead, and it will be automatically converted to a promise if necessary. gj has fewer such automatic conversions, so you end up needing to write Promise::ok(x)
quite frequently. So it's nice that it has a short easy-to-type name.
A previous version of gj used the names fulfilled
and rejected
, and at least in my subjective experience ok
and err
are a significant improvement.
Which brings up a related point: in gj terminology, any promise that is no longer pending is called "resolved"; it is "fulfilled" if it has a success value, and it is "rejected" if it has an error value, but in either case it is "resolved". I believe this terminology is consistent with the Promises/A+ standard. So one consequence of your proposal is that we would need a new word for what we currently call "resolved". Do you have any suggestions for such a word?