diff --git a/.changeset/great-fishes-look.md b/.changeset/great-fishes-look.md new file mode 100644 index 000000000..8a48a4fe1 --- /dev/null +++ b/.changeset/great-fishes-look.md @@ -0,0 +1,9 @@ +--- +"ember-resources": major +--- + +The `RemoteData` resource now has the same state changes and semantics as `trackedFunction`. + +Breaking Changes: + +- `isResolved` is only true when the request succeeds. During migration, you may use `isFinished` for previous behavior. diff --git a/ember-resources/src/util/remote-data.ts b/ember-resources/src/util/remote-data.ts index d16347d4c..4120b7a0d 100644 --- a/ember-resources/src/util/remote-data.ts +++ b/ember-resources/src/util/remote-data.ts @@ -26,11 +26,29 @@ export class State { */ @tracked status: null | number = null; + /** + * True if the request has succeeded + */ + @tracked isResolved = false; + + /** + * True if the request has failed + */ + @tracked isRejected = false; + /** * true if the request has finished */ - get isResolved() { - return Boolean(this.value) || Boolean(this.error); + get isFinished() { + return this.isResolved || this.isRejected; + } + + /** + * Alias for `isFinished` + * which is in turn an alias for `isResolved || isRejected` + */ + get isSettled() { + return this.isFinished; } /** @@ -44,14 +62,14 @@ export class State { * true if the fetch request is in progress */ get isLoading() { - return !this.isResolved; + return !this.isFinished; } /** * true if the request throws an exception */ get isError() { - return Boolean(this.error); + return this.isRejected; } } @@ -110,9 +128,11 @@ export function remoteData( return response.json(); }) .then((data) => { + state.isResolved = true; state.value = data; }) .catch((error) => { + state.isRejected = true; state.error = error; }) );