[Ramda] Refactor a Promise Chain to Function Composition using Ramda

Promise chains can be a powerful way to handle a series of transformations to the results of an async call. In some cases, additional promises are required along the way. In cases where there are no new promises, function composition can reduce the number of dot chained thens you need. In this lesson, we'll look at how to take a promise chain, and reduce it down with function composition.

 

复制代码
const deckUrl = 'https://deckofcardsapi.com/api/deck/new/shuffle/?cards=AS,2S,5C,3C,KD,AH,QH,2C,KS,8C';

fetch(deckUrl)
  .then(res => res.json())
  .then(deck =>  fetch(`https://deckofcardsapi.com/api/deck/${deck.deck_id}/draw/?count=10`)
        .then(res => res.json())
  .then(deck => deck.cards)
  .then(cards => cards.filter(c => c.suit === 'CLUBS'))
  .then(cards => cards.map(c => c.image))
  .then(cards => cards.sort((c1, c2) => c1.value - c2.value)))
  .then(cards => cards.map(u => `<img width="100" src="${u}"/>`).join(''))
  .then(imgString => {
   document.querySelector('#cards').innerHTML = `<div>${imgString}</div>`
  })
复制代码

 

 

We want to use Ramda to improve code:

Using R.prop and R.map:

复制代码
// from 
.then(deck => deck.cards)

// to
.then(prop('cards'))


// from
.then(cards => cards.map(c => c.image))

//to
.then(map(prop('image')))
复制代码

 

Using R.propEq and R.filter:

// from
.then(cards => cards.filter(c => c.suit === 'CLUBS'))

//to
.then(filter(propEq('suit', 'CLUBS')))

 

Using R.sortBy:

// from
.then(cards => cards.sort((c1, c2) => c1.value - c2.value)))

// to
.then(sortBy(prop('value'))))

 

Using R.compose:

// from
.then(cards => cards.map(u => `<img width="100" src="${u}"/>`).join(''))

// to
.then(compose(join(''), map(u => `<img width="100" src="${u}"/>`)))

 

Now it looks like:

复制代码
const {prop, filter, map, sortBy, propEq, join, compose, pluck} = R
const deckUrl = 'https://deckofcardsapi.com/api/deck/new/shuffle/?cards=AS,2S,5C,3C,KD,AH,QH,2C,KS,8C'
        
fetch(deckUrl)
  .then(res => res.json())
  .then(deck =>  fetch(`https://deckofcardsapi.com/api/deck/${deck.deck_id}/draw/?count=10`)
        .then(res => res.json())
  .then(prop('cards'))
  .then(filter(propEq('suit', 'CLUBS')))
  .then(map(prop('image')))
  .then(sortBy(prop('value')))
  .then(compose(join(''), map(u => `<img width="100" src="${u}"/>`)))
  .then(imgString => {
   document.querySelector('#cards').innerHTML = `<div>${imgString}</div>`
  })
复制代码

 

We can also pull out each step as a function.

复制代码
const {prop, filter, map, sortBy, propEq, join, compose, pluck} = R
const deckUrl = 'https://deckofcardsapi.com/api/deck/new/shuffle/?cards=AS,2S,5C,3C,KD,AH,QH,2C,KS,8C'
const getId = prop('deck_id');
const drawCards = id => fetch(`https://deckofcardsapi.com/api/deck/${id}/draw/?count=10`)
        .then(res => res.json());
const getCards = prop('cards');  
const justClubs = filter(propEq('suit', 'CLUBS'));
const sortByValue = sortBy(prop('value'));
const getImages = map(prop('image'));
const toImgString = compose(join(''), map(u => `<img width="100" src="${u}"/>`));
const render = imgString => {
   document.querySelector('#cards').innerHTML = `<div>${imgString}</div>`
  };
        
fetch(deckUrl)
  .then(res => res.json())
  .then(getId)
  .then(drawCards)
  .then(getCards)
  .then(justClubs)
  .then(getImages)
  .then(sortByValue)
  .then(toImgString)
  .then(render);
复制代码

 

Using R.pluck to replace R.map(R.prop(''));

const getImages = pluck('image');

 

复制代码
const {prop, filter, map, sortBy, propEq, join, compose, pluck} = R
const deckUrl = 'https://deckofcardsapi.com/api/deck/new/shuffle/?cards=AS,2S,5C,3C,KD,AH,QH,2C,KS,8C'
const getId = prop('deck_id');
const drawCards = id => fetch(`https://deckofcardsapi.com/api/deck/${id}/draw/?count=10`)
        .then(res => res.json());
const getCards = prop('cards');  
const justClubs = filter(propEq('suit', 'CLUBS'));
const sortByValue = sortBy(prop('value'));
const getImages = pluck('image');
const toImgString = compose(join(''), map(u => `<img width="100" src="${u}"/>`));
const render = imgString => {
   document.querySelector('#cards').innerHTML = `<div>${imgString}</div>`
  };
  
const transformData = compose(toImgString, getImages, sortByValue, justClubs, getCards)  
        
fetch(deckUrl)
  .then(res => res.json())
  .then(getId)
  .then(drawCards)
  .then(compose(render, transformData));
复制代码

 

posted @   Zhentiw  阅读(411)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2016-03-03 [AngualrJS] Using Angular-Cache for caching http request
2015-03-03 [Javascript] Introduce to Webpack
点击右上角即可分享
微信分享提示