Closed DrKillshot closed 5 days ago
const data = Data({ Success: (id: number, username: string) => ({id, username}), Failure: (error: string) => error, Pending: Empty }) const success = data.Success(10, "john.doe") // or const error = data.Failure("There has been a network failure") // or const pending = data.Pending success.match({ Success: ({id, username}) => console.log(`Payload received. {id: ${id}, username: ${username}}`), Failure: (error) => console.log(`There has been an error. Error: ${error}`), Pending: () => console.log("The request is still pending, please wait.") }) // or error.match({ Success: ({id, username}) => console.log(`Payload received. {id: ${id}, username: ${username}}`), Failure: (error) => console.log(`There has been an error. Error: ${error}`), Pending: () => console.log("The request is still pending, please wait.") }) // or pending.match({ Success: ({id, username}) => console.log(`Payload received. {id: ${id}, username: ${username}}`), Failure: (error) => console.log(`There has been an error. Error: ${error}`), Pending: () => console.log("The request is still pending, please wait.") }) // or if we don't want to define all cases success.match({ Pending: () => console.log("The request is still pending, please wait."), _: () => console.log("Default case") })
Use case example