propertly return promises, properly catch Promise.all failure

This commit is contained in:
Joe Fleming 2015-11-05 16:15:20 -07:00
parent 3faa130d67
commit 32e9201ba5

View file

@ -16,15 +16,14 @@ function ScenarioManager(server) {
* @return {Promise} A promise that is resolved when elasticsearch has a response * @return {Promise} A promise that is resolved when elasticsearch has a response
*/ */
ScenarioManager.prototype.load = function (id) { ScenarioManager.prototype.load = function (id) {
var self = this;
var scenario = config[id]; var scenario = config[id];
if (!scenario) return Promise.reject('No scenario found for ' + id); if (!scenario) return Promise.reject('No scenario found for ' + id);
var self = this;
return Promise.all(scenario.bulk.map(function mapBulk(bulk) { return Promise.all(scenario.bulk.map(function mapBulk(bulk) {
var loadIndexDefinition; var loadIndexDefinition;
if (bulk.indexDefinition) { if (bulk.indexDefinition) {
var body = require(path.join(scenario.baseDir, bulk.indexDefinition)); var body = require(path.join(scenario.baseDir, bulk.indexDefinition));
loadIndexDefinition = self.client.indices.create({ loadIndexDefinition = self.client.indices.create({
index: bulk.indexName, index: bulk.indexName,
body: body body: body
@ -33,10 +32,10 @@ ScenarioManager.prototype.load = function (id) {
loadIndexDefinition = Promise.resolve(); loadIndexDefinition = Promise.resolve();
} }
return loadIndexDefinition.then(function bulkRequest() { return loadIndexDefinition
.then(function bulkRequest() {
var body = require(path.join(scenario.baseDir, bulk.source)); var body = require(path.join(scenario.baseDir, bulk.source));
return self.client.bulk({
self.client.bulk({
body: body body: body
}); });
}); });
@ -69,7 +68,8 @@ ScenarioManager.prototype.unload = function (id) {
ScenarioManager.prototype.reload = function (id) { ScenarioManager.prototype.reload = function (id) {
var self = this; var self = this;
return this.unload(id).then(function load() { return self.unload(id)
.then(function load() {
return self.load(id); return self.load(id);
}); });
}; };
@ -103,14 +103,13 @@ ScenarioManager.prototype.loadIfEmpty = function (id) {
}) })
.then(function handleCountResponse(response) { .then(function handleCountResponse(response) {
if (response.count === 0) { if (response.count === 0) {
self.load(id); return self.load(id);
} }
}) })
.catch(function (reason) { }))
self.load(id); .catch(function (reason) {
}); return self.load(id);
})); });
}; };
module.exports = ScenarioManager;
module.exports = ScenarioManager;