Created
January 24, 2017 16:03
-
-
Save kharandziuk/a8e5e3a81b8e74a4c3310f615c93e099 to your computer and use it in GitHub Desktop.
strange flatmap behaviour
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require('superagent'); | |
const _ = require('lodash'); | |
const H = require('highland'); | |
const Promise = require('bluebird'); | |
function getLocations() { | |
const call = request | |
.get('http://viatorapi.viator.com/service/taxonomy/locations') | |
.query({apiKey: API_KEY}) | |
return Promise.resolve(call) | |
.then((res) => res.body.data) | |
} | |
const callDetail = (id) => | |
Promise.resolve( | |
request | |
.get('http://viatorapi.viator.com/service/attraction') | |
.query({apiKey: '2697384146408674701', seoId: id, currencyCode: 'EUR'}) | |
) | |
.then(x => x.body.data) | |
.then(function(x) { | |
if(_.isNull(x)) { | |
throw null | |
} | |
return x | |
}) | |
.catch(function(e) { | |
return Promise.delay(500).then(()=> callDetail(id)) | |
}) | |
function getAttractionsByLocation(id) { | |
return Promise.resolve( | |
request | |
.post('http://viatorapi.viator.com/service/search/attractions') | |
.query({apiKey: API_KEY}) | |
.send({destId: id}) | |
) | |
.then(res => res.body.data) | |
.catch(e => console.log(e)) | |
} | |
function populate(create) { | |
return H( | |
getLocations() | |
) | |
.flatten() | |
.map(x => x.destinationId) | |
.map(x => H(getAttractionsByLocation(x))) | |
.flatten() | |
.each(x => console.log(x.length)) | |
} | |
if (require.main === module) { | |
populate(console.log) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment