Skip to content

Instantly share code, notes, and snippets.

@watagashi
Last active December 16, 2015 15:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save watagashi/f9bec64dd78618487bb5 to your computer and use it in GitHub Desktop.
Save watagashi/f9bec64dd78618487bb5 to your computer and use it in GitHub Desktop.
Promise で実行順序を問わない場合
'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