-
-
Notifications
You must be signed in to change notification settings - Fork 17
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
async functions already allow this pattern, but only asyncly #45
Comments
If change the Promise.prototype.result = function() {
return this.then(x=>[null,x], err=>[err,null]);
}; will it be better ? const [error, data] = await asyncFunc().result(); Not for sync |
This is actually a really good pattern, we should discuss about this in #4 (preferred operator/keyword for safe assignment) |
There is no need to add anything to the Promise prototype. The current Currently let result = new Array(2);
try {
result[1] = await somePromise; // the expression goes here
} catch (e) {
result[0] = e;
} Shortening all of that down to one expression keyword and adding the const assignment is quite epic. No further polyfill is needed. |
In fact, I've added a try method to the Promise prototype in one of my projects.
|
@Arlen22 please don't ever do that - that's the kind of thing that obstructs language evolution. |
No worries. I entirely agree. I am certainly not putting that online anywhere. I could accomplish the same thing with |
Async functions already basically allow this pattern. An async function returns a promise, and any synchronous exception thrown causes the promise to return a rejected promise.
A call to an async function using this pattern looks like
const [error, data] = await asyncFunc().then(e => [undefined, e], e => [e, undefined]);
. The only advantage of turning this into an operator is that typescript can handle the typing of this properly (removing undefined from the value type if the error is undefined).But since making every single Javascript function async doesn't really make sense, this is still a useful operator to have. In fact, it may even bring a compelling use case to the sync world.
The text was updated successfully, but these errors were encountered: