[Compose] 18. Maintaining structure whilst asyncing
2016-12-22 20:38
337 查看
We take our Promise.all() analogy further by using traversable on a Map(). Then we use two traversals in the same workflow.
Traverse is good for maintaning the original data structure:
For example, the code here return Map({Task}) instead of Map({}).
So we can actually replace map with traverse.
Now we are able to keep the same structure as before.
We can also use double traverse if needed, for example we change data to array of path inside of string:
Because Array doesn't have traverse function, so we need to use List from immutable.
Traverse is good for maintaning the original data structure:
const Task = require('data.task'); const { List, Map } = require('immutable-ext'); const httpGet = (path, params) => Task.of(`${path} result`); // map const res0 = Map( { home : '/', about : '/about' } ).map(route => httpGet(route, {}) ); console.log(res0); // Map{ home: Task(), about: Task()}
For example, the code here return Map({Task}) instead of Map({}).
So we can actually replace map with traverse.
// Traverse Map( { home: '/', about: '/about' } ).traverse( Task.of, route => httpGet(route, {}) ).fork(console.error, console.log); // Map { "home": "/ result", "about": "/about result" }
Now we are able to keep the same structure as before.
We can also use double traverse if needed, for example we change data to array of path inside of string:
// Double traverse Map( { home: ['/', '/home'], about: ['/about', '/help'] } ).traverse( Task.of, routes => List(routes) .traverse( Task.of, route => httpGet(route, {}) ) ).fork( console.error, console.log ); // Map { "home": List [ "/ result", "/home result" ], "about": List [ "/about result", "/help result" ] }
Because Array doesn't have traverse function, so we need to use List from immutable.
相关文章推荐
- 18.Examine the structure of the PROMOTIONS table:
- 20非常有用的Java程序片段(16-18)
- 从零单排c++ primer(18)
- docker, lxc, docker-compose, juju
- 【DataStructure&AlgorithmInJava】Ch02-HighArrayDemo
- OpenCV18(图像拼接stitcher_detail)
- pat Data_Structure 前缀表达式的计算--堆栈法
- Android学习笔记进阶18之画图并保存图片到本地
- hdu Data Structure? 线段树
- 动态规划练习18:买书
- Extra Credits: The Skinner Box 18
- Lecture 18 Shortest Paths II
- [Phonegap+Sencha Touch] 移动开发18 Sencha Touch项目通过phonegap打包后的程序名字的问题
- 剑指offer-面试题 18:树的子结构
- 学拳录 18青龙出水
- 面试18之猫狗队列
- OCP 1Z0 052 18
- 《C语言及程序设计》教学视频 <18 链表中结点的插入和删除> 示例代码
- Cocos2d-x学习笔记(18)(TestCpp源码分析-2)