Last active
December 16, 2015 15:17
-
-
Save watagashi/f9bec64dd78618487bb5 to your computer and use it in GitHub Desktop.
Promise で実行順序を問わない場合
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
'use strict'; | |
var fs = require('fs'); | |
var program = require('commander'); | |
var GoogleMapsAPI = require('googlemaps'); | |
var gapiConf = { | |
key: 'google API key', | |
secure: true | |
}; | |
var gmAPI = new GoogleMapsAPI(gapiConf); | |
var geocodeParams = { | |
language: 'ja', | |
region: 'ja' | |
}; | |
var data = JSON.parse(fs.readFileSync('data.json')); | |
var outputFile = 'data.json.out'; | |
function getLocation(data, i) { | |
return new Promise(function(resolve, reject) { | |
var address = data[i].address; | |
geocodeParams.address = address; | |
gmAPI.geocode(geocodeParams, function(err, result) { | |
if (err) { | |
console.error('[' + i + ']', 'Parse failed: ', address); | |
reject(err); | |
} else { | |
var location = result.results[0].geometry.location; | |
console.log('[' + i + ']',location); | |
resolve(location); | |
} | |
}); | |
}); | |
} | |
var tasks = []; | |
for (var i = 0; i < data.length; i++) { | |
tasks.push(getLocation(data, i)); | |
} | |
Promise.all(tasks).catch(function(e) { | |
console.error(e); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment