首页 > 其他分享 >[Compose] Async generator, Promise + generator

[Compose] Async generator, Promise + generator

时间:2023-10-26 14:56:08浏览次数:36  
标签:Compose run generator res next var Async getData

function getData(d) {
    setTimeout(() => {
        if (typeof d === "number") {
            run.next(d/2)
        } else {
            run.next(d)
        }
    }, 500)
}

function* gen() {
    var x = 1 + (yield getData(10)) // x = 1 + 5
    var y = 1 + (yield getData(30)) // y = 1 + 15
    var answer = (yield getData("Meaning of life: " + (x + y))) 
    console.log(answer)
}

var run = gen()
run.next()

Async generator able to pasue the execution and fetching data from server then resume the execution.

We writ synchronous, sequential, blocking looking code, let generator to handle all the async stuff.

Async generator can be a good tool when you want to concat all the async operations but write it in sync code.

 

But in previous code, there is inversion of control issue. how can we truch run.next()called only once? we don't have control over it.

 

Promise + generator

function getData(d) {
    return new Promise((res) => {
        setTimeout(() => {
            console.log("resolve")
            if (typeof d === "number") {
                res(d/2)
            } else {
                res(d)
            }
        }, 100)
    })
}

function* gen() {
    var x = 1 + (yield getData(10).then((x) => run.next(x))
    var y = 1 + (yield getData(30).then((x) => run.next(x))
    var answer = (yield getData("Meaning of life: " + (x + y)).then((x) => run.next(x)))
    console.log(answer)
}

var run = gen()
run.next()

And async..await is a short hand syntax of it.

function getData(d) {
    return new Promise((res) => {
        setTimeout(() => {
            console.log("resolve")
            if (typeof d === "number") {
                res(d/2)
            } else {
                res(d)
            }
        }, 100)
    })
}

async function gen() {
    var x = 1 + await getData(10)
    var y = 1 + await getData(30)
    var answer = await getData("Meaning of life: " + (x + y))
    console.log(answer)
}

gen()

 

标签:Compose,run,generator,res,next,var,Async,getData
From: https://www.cnblogs.com/Answer1215/p/17789416.html

相关文章

  • docker-compose部署SASL认证的kafka
    前言测试服务器:10.255.60.149一.编写docker-compose文件1.docker-compose.ymlversion:'3.8'services:zookeeper:image:wurstmeister/zookeepervolumes:-/data/zookeeper/data:/data-/home/docker-compose/kafka/config:/opt/zookeeper-......
  • docker-compose: command not found问题的两种常用方法
    docker-compose:commandnotfounddocker-compose是什么Compose定位是「定义和运行多个Docker容器的应用(Definingandrunningmulti-containerDockerapplications)」,其前身是开源项目Fig。在日常工作中,经常会碰到需要多个容器相互配合来完成某项任务的情况。例如要实现一个......
  • docker-compose 外部配置部署 java 项目原创
    有的项目写完,需要打包到不同的环境。所以配置一个外部yml配置文件会方便很多,不用重新打包。文件目录构造├──app├──application.yml├──app.jar├──Dockerfile├──mysql├──nginxdocker-compose.ymlw.sh复制DockerfileFROMjava:8#VO......
  • Compose动画原理-我的一点小思考
    思想Compose的动画系统是基于值系统的动画,和传统的基于回调的动画不同,Compose的动画api通常对外暴露一个可观察的随时间改变的状态,进而驱动重组或者重绘,从而达成动画的效果基本使用可见性动画使用AnimatedVisibility,内容尺寸动画用animateContentSize,根据不同的状态展示不同的Com......
  • docker-compose安装 es 和 kibana
    1、docker-compose.ymlversion:'3'services:es_01:image:elasticsearch:7.1.0container_name:es_01environment:#以单一节点模式启动-discovery.type=single-node#设置使用jvm内存大小-ES_JAVA_OPTS=-Xms128m-Xmx512m......
  • docker入门加实战—项目部署之DockrCompose
    docker入门加实战—项目部署之DockrCompose我们部署一个简单的java项目,可能就包含3个容器:MySQLNginxJava项目而稍微复杂的项目,其中还会有各种各样的其它中间件,需要部署的东西远不止3个。如果手动的逐一部署,就太麻烦了,同时也无法保证完整性。而DockerCompose就可以帮助我......
  • Docker-Compose
    目录1.简介2.Composeyml规则3.Compose示例3.1Python-web应用3.2wordpress应用1.简介DockerCompose是Docker官方的开源项目,作用是通过docker-compose.yml定义运行多个容器官方文档Compose概念:Services:容器,应用,例如:web、redis、mysql...Project:一组关联的容......
  • async await
    async和await是JavaScript中用于处理异步操作的特性,它们使异步代码更易于编写和理解。下面我将详细解释它们,并提供一些示例来说明它们的用法。async函数:async关键字用于定义一个异步函数。异步函数是返回一个Promise对象的函数。在异步函数内部,你可以使用await来等......
  • docker-compose
    Compose简介Compose项目是Docker官方的开源项目,负责实现对Docker容器集群的快速编排。从功能上看,跟OpenStack中的Heat十分类似。其代码目前在https://github.com/docker/compose上开源。Compose定位是「定义和运行多个Docker容器的应用(Definingandrunnin......
  • docker-compose 安装 etcd
    目录docker-compose.yamldocker-compose.yamlversion:"3"services:etcd:hostname:etcdimage:bitnami/etcd:3deploy:replicas:1restart_policy:condition:on-failure#ports:#-"2379:2379"......