首页 > 其他分享 >[Compose] Async programming: Thunks

[Compose] Async programming: Thunks

时间:2023-10-18 15:00:57浏览次数:39  
标签:function Compose return cb cache programming thunk time Async

Thunks

Sync thunk: A blocker of code which has everything ready and can return the value directly.

function add(x, y) {
  return x + y
}

const thunk = function() {
  return add(10, 15)
}

thunk() // 25

Ao thunkis pretty much the same as High order function, it can defers the result of another function call, so that you can pass thunk around to deliever the result of that function call. Notice that, the result of the function call can change over time. For example, instead of retrun add(10, 15), return Date.now(). Also thunk is similar to Promise as well.

 

Async thunk: instead of passing params to get value back, now you passing a function, and wait it to be invoke.

function addAsync(x, y, cb) {
  setTimeout(() => {
     cb(x + y)
  }, 1000)
}

const thunk = function(cb) {
  return addAsync(10, 15, cb)
}

thunk((sum) => {
    console.log(sum)
}) // 25

The power of async think is we have control over how thing is working inside thunk.

let cache = null
const thunk = function(cb) {
  if (cache) {
    return cache
  }
  cache = addAsync(10, 15, cb)
  return cache
}

For example we can add cache, the first time call the thunk might take a while to do data fetching and calcaultion, but second time it return the results right away.

Which means for consumer, you might find that calling thunk multi times, it might return the data in different time durion. Aysnc thunk normalize the time, for consumer, they don't need to worry about how the callback should be invokded. It becomes time independent.

 

Helper method to make thunk:

function makeThunk(fn) {
  var args = [].slice.call(arguments, 1)
  return function(cb) {
    args.push(cb)
    fn.apply(null, args)
  }
}

Usage:

function addAsync(x, y, cb) {
  setTimeout(() => {
     cb(x + y)
  }, 1000)
}

const thunk = makeThunk(addAsync, 10, 15)

thunk((sum) => {
    console.log(sum)
}) // 25

 

标签:function,Compose,return,cb,cache,programming,thunk,time,Async
From: https://www.cnblogs.com/Answer1215/p/17772356.html

相关文章

  • Ubuntu 中Docker Compose的奇淫异巧
    在本教程中,我们将看到如何在Ubuntn16.04上安装DockerCompose。安装Docker我们需要安装Docker来安装DockerCompose。首先为官方Docker仓库添加公钥。$curl-fsSLhttps://download.docker.com/linux/ubuntu/gpg|sudoapt-keyadd-接下来,添加Docker仓库......
  • Japan Registry Services (JPRS) Programming Contest 2023 (AtCoder Beginner Contes
    JapanRegistryServices(JPRS)ProgrammingContest2023(AtCoderBeginnerContest324)赛后总结可悲的是:我没来得及写题解。T1Same秒切。直接输入排一遍序再遍历即可。#include<bits/stdc++.h>usingnamespacestd;intn,a[101];intmain(){cin>>n;f......
  • 比赛总结:Japan Registry Services (JPRS) Programming Contest 2023 (AtCoder Beginn
    比赛:JapanRegistryServices(JPRS)ProgrammingContest2023(AtCoderBeginnerContest324)A-same1.常规方法intmain(){ intn; cin>>n; vector<int>s(n);//利用vector容器可以不需要确定内存大小 for(auto&n:s) { cin>>n; } for(inti=0;i......
  • [Compose] Callback is not suitable for Async programming
    Anexampleofcallbackimplemnetationforhandlingasyncflow:functionfakeAjax(url,cb){varfake_responses={file1:"Thefirsttext",file2:"Themiddletext",file3:"Thelasttext",};varrandomDela......
  • js Promise、generator、async/await
    1.Promise的出现是为了解决ajax回调地狱的问题,但是Promise的链式调用看起来也不太美观。2.generator的出现就是为了让异步流程看起来更直观。3.然而generator在定义的时候是直观的,在执行的时候又会面临回调地狱的问题,所以async/await应运而生,async/await可以直接......
  • Programming abstractions in C阅读笔记:p179-p180
    《ProgrammingAbstractionsInC》学习第60天,p179-p180总结。一、技术总结1.palindrome(回文)(1)包含单个字符的字符串(如"a"),或者空字符串(如"")也是回文。(2)示例:“level”、"noon"。2.predicatefunction(1)predicate的意思pre-("forth")+*deik-("show"),“t......
  • 2022 China Collegiate Programming Contest (CCPC) Guilin Site(持续更新)
    Preface由于还有两周就要滚去打区域赛了,这周开始周末每天都训一场吧这场总体来说打的还可以,虽然E题这个Easy从卡局卡到3h,但由于其它的题都是一遍过所以罚时还尚可跻进Au区后面一个小时看徐神和祁神苦战K大分类讨论,虽然场下感觉摸了一个B的做法出来,但感觉实现还是太麻烦了就没写......
  • Deep Learning —— 异步优化器 —— RMSpropAsync —— 异步RMSprop
       ============================================  代码地址:https://github.com/chainer/chainerrl/blob/master/chainerrl/optimizers/rmsprop_async.py defupdate_core_cpu(self,param):grad=param.gradifgradisNone:......
  • label-studio docker-compose 运行试用
    label-studio是一个支持多格式的数据标注工具,以下是基于docker-compose运行的试用环境准备docker-composeversion:"3.9"services:nginx:image:heartexlabs/label-studio:latestports:-"8080:8085"-"8081:8086"......
  • 2021 China Collegiate Programming Contest (CCPC) Guilin Site
    A.AHeroNamedMagnus#include<bits/stdc++.h>usingnamespacestd;#defineintlonglongusingpii=pair<int,int>;usingvi=vector<int>;voidsolve(){intx;cin>>x;cout<<2ll*x-1<<"......