Skip to content

Avoid extending built-in JS classes #175

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

Merged
merged 2 commits into from
Oct 11, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions packages/react-async/src/reducer.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
import { getInitialStatus, getIdleStatus, getStatusProps, statusTypes } from "./status"

// This exists to make sure we don't hold any references to user-provided functions
class NeverSettle extends Promise {
constructor() {
super(() => {}, () => {})
/* istanbul ignore next */
if (Object.setPrototypeOf) {
// Not available in IE 10, but can be polyfilled
Object.setPrototypeOf(this, NeverSettle.prototype)
}
}
// The way NeverSettle extends from Promise is complicated, but can't be done differently because Babel doesn't support
// extending built-in classes. See https://babeljs.io/docs/en/caveats/#classes
function NeverSettle() {}
/* istanbul ignore next */
if (Object.setPrototypeOf) {
Object.setPrototypeOf(NeverSettle, Promise)
} else {
NeverSettle.__proto__ = Promise
}
NeverSettle.prototype = Object.assign(Object.create(Promise.prototype), {
finally() {
return this
}
},
catch() {
return this
}
},
then() {
return this
}
}
},
})

export const neverSettle = new NeverSettle()

Expand Down