generated from tc39/template-for-proposals
-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
polyfill.js
28 lines (24 loc) · 861 Bytes
/
polyfill.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
Symbol.result = Symbol("result")
Function.prototype[Symbol.result] = function (...args) {
try {
const result = this.apply(this, args)
// Handles recursive cases, like async function() {}
// or user made implementations like function() { return objectWithSymbolResult }
if (result && typeof result === "object" && 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
return [error || new Error("Thrown error is falsy")]
}
}
Promise.prototype[Symbol.result] = async function () {
try {
const result = await this
return [null, result]
} catch (error) {
return [error || new Error("Thrown error is falsy")]
}
}