Skip to content

Best way to use effect to work around async computed? #52

Closed Answered by rodydavis
btrautmann asked this question in Q&A
Discussion options

You must be logged in to vote

In 1.5.0 introduced AsyncSignal which should make async computed a lot easier to achieve:

import 'package:signals/signals.dart';

void main() {
  Future<String> fetch(int id) async {
    await Future.delayed(const Duration(milliseconds: 5));
    return '$id';
  }

  Stream<int> idChanges() async* {
    yield 1;
    await Future.delayed(const Duration(milliseconds: 5));
    yield 2;
    await Future.delayed(const Duration(milliseconds: 5));
    yield 3;
  }

  final id = asyncSignalFromStream(idChanges, initialValue: 0);
  final user = fetch(id.value).toSignalWithDefault('guest');
  final greeting = computed(() => 'Hello, ${user.value}');

  effect(() {
    print('current id: $id');
    user.

Replies: 5 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by rodydavis
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants