-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequests-flow.js
66 lines (56 loc) · 1.89 KB
/
requests-flow.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
'use strict';
const VError = require('verror');
const axios = require('axios');
const flow = require('../lib');
const Utils = require('./utils');
const { Task } = flow;
const context = {
baseUrl: 'https://jsonplaceholder.typicode.com',
postsPath: '/posts',
usersPath: '/users'
};
const getUsers = Task.create('users', ctx => {
return axios.get(ctx.baseUrl + ctx.usersPath)
.then(response => response.data);
})
.pipe(Utils.tapLog('users list'))
.pipe((ctx, users) => users.map(user => ({ id: user.id, username: user.username })))
.pipe(Utils.tapLog('transformed users list'));
const getPosts = (ctx, userId) => {
return axios.get(`${ctx.baseUrl + ctx.postsPath}?userId=${userId}`)
.then(response => response.data);
};
const getUsersPosts = (ctx, users) => {
console.time('get posts run time');
const getPostsTasks = users.map(user => {
return Task.create(user.username, getPosts, user.id)
.pipe(Utils.tapLog(`${user.username}: posts`))
.pipe((ctx, posts) => {
return posts.map(post => post.title);
})
.pipe(Utils.tapLog(`${user.username}: posts' titles`));
});
return flow.parallel(getPostsTasks, { name: 'posts', concurrency: getPostsTasks.length, resultsAsArray: false })
.run(ctx)
.then(data => {
console.timeEnd('get posts run time');
return data;
});
};
const usersFlow = flow({ getUsers }, { name: 'users', resultsAsArray: true });
console.time('get users run time');
usersFlow.run(context)
.then(data => {
console.timeEnd('get users run time');
Utils.prettyPrint('users flow results', data.results);
return getUsersPosts(data.context, data.results);
})
.then(data => {
Utils.prettyPrint('users posts results', data.results);
})
.catch(error => {
// error = TaskError, a VError instance
console.error(VError.fullStack(error));
// The error's cause
console.error(error.cause());
});