Skip to content
This repository has been archived by the owner on Nov 20, 2019. It is now read-only.

Commit

Permalink
ES5😞
Browse files Browse the repository at this point in the history
  • Loading branch information
cn0047 committed Jun 22, 2017
1 parent 9ad0800 commit 9a60080
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

import fetch from 'node-fetch';
const fetch = require('node-fetch');

/**
* Fetches data from API.
Expand All @@ -9,10 +7,14 @@ import fetch from 'node-fetch';
* @param {string} url Url to API endpoint.
* @param {function} cb Callback function, which will receive data.
*/
const fetchData = (resourceName, url, cb) => {
const fetchData = function(resourceName, url, cb) {
fetch(url)
.then(res => res.json())
.then(json => cb({resourceName: resourceName, data: json}))
.then(function(res) {
return res.json();
})
.then(function(json) {
cb({resourceName: resourceName, data: json});
})
;
};

Expand All @@ -23,26 +25,30 @@ const fetchData = (resourceName, url, cb) => {
* @param {object} res Response object.
* @param {function} cb Callback function, which will receive array with all obtained data.
*/
export default (req, res, cb) => {
module.exports = function(req, res, cb) {

let host = req.protocol + '://' + req.get('host') + '/';
let promises = [];
var host = req.protocol + '://' + req.get('host') + '/';
var promises = [];

// Each parameter contains endpoint URL,
// so will use it as part of target URL.
for (let resourceName in req.query) {
for (var resourceName in req.query) {
// Url to Api endpoint.
let url = host + req.query[resourceName];
promises.push(new Promise(resolve => {
fetchData(resourceName, url, data => resolve(data));
var url = host + req.query[resourceName];
promises.push(new Promise(function(resolve) {
fetchData(resourceName, url, function(data) {
resolve(data);
});
}));
}

Promise.all(promises).then(data => {
let result = {};
Promise.all(promises).then(function(data) {
var result = {};
// Format data into object,
// where key - is resource name, and value - data obtained from API.
data.forEach(el => result[el.resourceName] = el.data);
data.forEach(function(el) {
result[el.resourceName] = el.data;
});
cb(result);
});

Expand Down

0 comments on commit 9a60080

Please sign in to comment.