generated from tc39/template-for-proposals
-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d5970ed
commit 1b5e1cf
Showing
4 changed files
with
85 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
Symbol.result = Symbol("result") | ||
|
||
Function.prototype[Symbol.result] = function (...args) { | ||
try { | ||
const result = this(args) | ||
|
||
// Handles recursive cases, like async function() {} | ||
// or user made implementations like function() { return objectWithSymbolResult } | ||
if (Symbol.result in result) { | ||
return result[Symbol.result]() | ||
} | ||
|
||
return [null, result] | ||
} catch (error) { | ||
// throw undefined would break the pattern of destructuring the result type. | ||
// in [error, data], both error and data would be undefined | ||
if (!error) { | ||
return [new Error("Thrown error is falsy")] | ||
} | ||
|
||
return [error] | ||
} | ||
} | ||
|
||
Promise.prototype[Symbol.result] = async function () { | ||
try { | ||
return [null, await this] | ||
} catch (error) { | ||
if (!error) { | ||
return [new Error("Thrown error is falsy")] | ||
} | ||
|
||
return [error] | ||
} | ||
} |