首页 > 其他分享 >[Compose] Asynchronous Reactive Data with Promises

[Compose] Asynchronous Reactive Data with Promises

时间:2023-10-04 16:04:31浏览次数:37  
标签:Promises Compose callback value Reactive resolve key data wait

Let’s make using the observers asynchronous! This way we can update the data and have multiple observers run asynchronously.

class AsyncData {
  constructor(initialData) {
    this.data = initialData;
    this.subscribers = [];
  }

  // Subscribe to changes in the data
  subscribe(callback) {
    if (typeof callback !== 'function') {
      throw new Error('Callback must be a function');
    }
    this.subscribers.push(callback);
  }

  // Update the data and wait for all updates to complete
  async set(key, value) {
    this.data[key] = value;

    // Call the subscribed function and wait for it to resolve
    const updates = this.subscribers.map(async (callback) => {
      await callback(key, value);
    });

    await Promise.allSettled(updates);
  }
}

 

Let’s say we want to wait until all subscriptions to our asynchronous reactive data are processed:

const data = new AsyncData({ pizza: 'Pepperoni' });

data.subscribe(async (key, value) => {
  await new Promise(resolve => setTimeout(resolve, 500));
  console.log(`Updated UI for ${key}: ${value}`);
});

data.subscribe(async (key, value) => {
  await new Promise(resolve => setTimeout(resolve, 1000));
  console.log(`Logged change for ${key}: ${value}`);
});

// function to update data and wait for all updates to complete
async function updateData() {
  await data.set('pizza', 'Supreme'); // This will call the subscribed functions and wait for their promises to resolve
  console.log('All updates complete.');
}

updateData();

 

标签:Promises,Compose,callback,value,Reactive,resolve,key,data,wait
From: https://www.cnblogs.com/Answer1215/p/17742309.html

相关文章

  • 什么是 Angular 14 的 strict typing of Angular Reactive Forms
    Angular14引入的"stricttypingofAngularReactiveForms"是一项强大的功能,它进一步提高了Angular应用程序的类型安全性和可维护性,特别是在处理表单时。这个功能使开发人员能够更精确地定义表单控件和表单模型的类型,从而减少了潜在的运行时错误,并提供了更好的代码提示和文......
  • Compose基础示例(列表,状态,Image,Text Field, 定时器)
    @file:Suppress("UNREACHABLE_CODE")packagecom.by.composeappimportandroid.os.Bundleimportandroid.util.Logimportandroid.widget.Toastimportandroidx.activity.ComponentActivityimportandroidx.activity.compose.setContentimportandroidx.co......
  • Compose中的定时器
    /***定时器*/@Composablefuntime(){vartimer:Timer?=nullvalsdf=SimpleDateFormat("yyyy-MM-ddHH:mm:ss")timer=Timer()varnumberbyremember{mutableStateOf(sdf.format(Date()))}LaunchedEffect(key1......
  • Docker V24 及 Docker Compose V2 的安装及使用
    前言Docker是一款流行的开源容器化平台,使用Docker可以有效地隔离应用程序和系统环境,使得应用程序在不同的环境中具有相同的行为DockerCompose是一个用于定义和管理多个Docker容器的工具Docker官方文档:文档地址DockerHub:容器镜像库和社区作者:易墨发布时间:2023.1......
  • Nginx简介与Docker Compose部署指南
    Nginx是一款高性能的开源Web服务器和反向代-理服务器,以其卓越的性能、可伸缩性和灵活性而闻名。它在全球范围内广泛用于托管Web应用程序、负载均衡、反向代-理和更多场景中。在本文中,我们将首先介绍Nginx的基本概念,然后演示如何使用DockerCompose轻松部署Nginx服务器。什么是Nginx......
  • docker compose和docker swarm 和 docker stack
    dockercompose:单机部署,使用dockercompose编排多个服务dockerswarm:多机部署,实现对单个服务的简单部署(通过dockerfile)dockerstack:实现集群环境下多服务编排。(通过compose.yml)狂神说docker(最全笔记)_狂神说docker笔记-CSDN博客          ......
  • 简化任务调度与管理:详解XXL-Job及Docker Compose安装
    在现代应用程序开发中,任务调度和管理是至关重要的一部分。XXL-Job是一个强大的分布式任务调度平台,它使得任务的调度和管理变得更加轻松和高效。本文将介绍XXL-Job的基本概念,并详细演示如何使用DockerCompose进行快速安装和配置。什么是XXL-Job?github地址:https://github.com/xuxue......
  • freeipa docker compose部署
    dockercompose文件version:"3.3"services:freeipa:image:freeipa/freeipa-server:centos-7container_name:freeipadomainname:freeipa.default.cncontainer_name:freeipa_idcnetworks:my_macvlan_net:ipv4_addr......
  • docker-compose部署rabbitmq
    docker-compose部署rabbitmqdocker-compose部署rabbitmq单机创建一个rabbitmq.yml的文件version:'3'services:rabbit:image:rabbitmq:3.8-managementhostname:rabbitcontainer_name:"rabbitmq3.8"restart:alwaysports:-......
  • uniapp自动引入Vue3(ref,reactive...)的API、uniapp生命周期和封装hooks
    未自动导入Vue3(ref,reactive...)的API和uniapp生命周期,需要在每个页面把API和uniapp生命周期的代码都重复写一遍<scriptsetup>import{ref,reactive}from"vue"import{onLaunch,onShow,onHide}from'@dcloudio/uni-app'//封装的hooksimport{useLi......