proposal-signals / signal-utils

MIT License
69 stars 6 forks source link

Signal-based automatic async function #19

Closed NullVoxPopuli closed 3 months ago

NullVoxPopuli commented 3 months ago
import { Signal } from 'signal-polyfill';
import { signalFunction } from 'signal-utils/async-function';

const url = new Signal.State('...');
const signalResponse = signalFunction(async () => {
  const response = await fetch(url.get()); // entangles with `url`
  // after an away, you've detatched from the signal-auto-tracking
  return response.json(); 
});

// output: true
// after the fetch finishes
// output: false
<template>
  <output>{{signalResponse.isLoading}}</output>
</template>

the signalResponse object has familiar properties on it: