mirror of
https://github.com/lainbo/component-party.git
synced 2026-04-05 04:59:02 +08:00
chore(lit): fetch data, extract function to create task in a separate file (#279)
Co-authored-by: Giuseppe Piscopo <g.piscopo@braincrumbz.com>
This commit is contained in:
@@ -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: () => [],
|
||||
});
|
||||
}
|
||||
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user