Skip to content

Commit

Permalink
chore(lit): fetch data, extract function to create task in a separate…
Browse files Browse the repository at this point in the history
… file
  • Loading branch information
GiuseppePiscopo committed Nov 15, 2024
1 parent f787cdd commit 6e229f5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
14 changes: 14 additions & 0 deletions content/7-webapp-features/2-fetch-data/lit/fetch-users-task.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Task } from "@lit/task";

export function createFetchUsersTask(element) {
return new Task(element, {
task: async () => {
const response = await fetch("https://randomuser.me/api/?results=3");
if (!response.ok) {
throw new Error(response.status);
}
return response.json();
},
args: () => [],
});
}
13 changes: 2 additions & 11 deletions content/7-webapp-features/2-fetch-data/lit/x-app.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
import { Task } from "@lit/task";
import { LitElement, html } from "lit";
import { customElement } from "lit/decorators.js";
import { createFetchUsersTask } from "./fetch-users-task";

@customElement("x-app")
export class XApp extends LitElement {
fetchUsers = new Task(this, {
task: async () => {
const response = await fetch("https://randomuser.me/api/?results=3");
if (!response.ok) {
throw new Error(response.status);
}
return response.json();
},
args: () => [],
});
fetchUsers = createFetchUsersTask(this);

render() {
return this.fetchUsers.render({
Expand Down

0 comments on commit 6e229f5

Please sign in to comment.