diff --git a/lib/main.js b/lib/main.js index 45716deb..808b7ea5 100644 --- a/lib/main.js +++ b/lib/main.js @@ -614,11 +614,11 @@ Lambda.prototype._listEventSourceMappings = function (lambda, params, cb) { }) } -Lambda.prototype._updateEventSources = function (lambda, functionName, existingEventSourceList, eventSourceList, cb) { +Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourceList, eventSourceList, cb) => { if (eventSourceList == null) { return cb(null, []) } - var updateEventSourceList = [] + const updateEventSourceList = [] // Checking new and update event sources for (let i in eventSourceList) { let isExisting = false @@ -668,29 +668,38 @@ Lambda.prototype._updateEventSources = function (lambda, functionName, existingE } } - return async.map(updateEventSourceList, function (updateEventSource, _cb) { + return Promise.all(updateEventSourceList.map((updateEventSource) => { switch (updateEventSource['type']) { case 'create': delete updateEventSource['type'] - lambda.createEventSourceMapping(updateEventSource, function (err, data) { - return _cb(err, data) + return new Promise((resolve, reject) => { + lambda.createEventSourceMapping(updateEventSource, (err, data) => { + if (err) return reject(err) + resolve(data) + }) }) - break case 'update': delete updateEventSource['type'] - lambda.updateEventSourceMapping(updateEventSource, function (err, data) { - return _cb(err, data) + return new Promise((resolve, reject) => { + lambda.updateEventSourceMapping(updateEventSource, (err, data) => { + if (err) return reject(err) + resolve(data) + }) }) - break case 'delete': delete updateEventSource['type'] - lambda.deleteEventSourceMapping(updateEventSource, function (err, data) { - return _cb(err, data) + return new Promise((resolve, reject) => { + lambda.deleteEventSourceMapping(updateEventSource, (err, data) => { + if (err) return reject(err) + resolve(data) + }) }) - break } - }, function (err, results) { - return cb(err, results) + return Promise.resolve() + })).then((data) => { + cb(null, data) + }).catch((err) => { + cb(err) }) } diff --git a/test/main.js b/test/main.js index a34a235f..54283bee 100644 --- a/test/main.js +++ b/test/main.js @@ -874,50 +874,62 @@ describe('lib/main', function () { ) }) - it('simple test with mock (In case of new addition)', (done) => { + it('simple test with mock (In case of new addition)', () => { program.eventSourceFile = 'event_sources.json' const eventSourceList = lambda._eventSourceList(program) - lambda._updateEventSources( - awsLambda, - 'functionName', - [], - eventSourceList.EventSourceMappings, - (err, results) => { - assert.isUndefined(err) - assert.deepEqual(results, [lambdaMockSettings.createEventSourceMapping]) - done() + return new Promise((resolve) => { + lambda._updateEventSources( + awsLambda, + 'functionName', + [], + eventSourceList.EventSourceMappings, + (err, results) => resolve({ err: err, results: results }) + ) + }).then((actual) => { + const expected = { + err: null, + results: [lambdaMockSettings.createEventSourceMapping] } - ) + assert.deepEqual(actual, expected) + }) }) - it('simple test with mock (In case of deletion)', (done) => { - lambda._updateEventSources( - awsLambda, - 'functionName', - lambdaMockSettings.listEventSourceMappings.EventSourceMappings, - {}, - (err, results) => { - assert.isUndefined(err) - assert.deepEqual(results, [lambdaMockSettings.deleteEventSourceMapping]) - done() + it('simple test with mock (In case of deletion)', () => { + return new Promise((resolve) => { + lambda._updateEventSources( + awsLambda, + 'functionName', + lambdaMockSettings.listEventSourceMappings.EventSourceMappings, + {}, + (err, results) => resolve({ err: err, results: results }) + ) + }).then((actual) => { + const expected = { + err: null, + results: [lambdaMockSettings.deleteEventSourceMapping] } - ) + assert.deepEqual(actual, expected) + }) }) - it('simple test with mock (In case of update)', (done) => { + it('simple test with mock (In case of update)', () => { program.eventSourceFile = 'event_sources.json' const eventSourceList = lambda._eventSourceList(program) - lambda._updateEventSources( - awsLambda, - 'functionName', - lambdaMockSettings.listEventSourceMappings.EventSourceMappings, - eventSourceList.EventSourceMappings, - (err, results) => { - assert.isUndefined(err) - assert.deepEqual(results, [lambdaMockSettings.updateEventSourceMapping]) - done() + return new Promise((resolve) => { + lambda._updateEventSources( + awsLambda, + 'functionName', + lambdaMockSettings.listEventSourceMappings.EventSourceMappings, + eventSourceList.EventSourceMappings, + (err, results) => resolve({ err: err, results: results }) + ) + }).then((actual) => { + const expected = { + err: null, + results: [lambdaMockSettings.updateEventSourceMapping] } - ) + assert.deepEqual(actual, expected) + }) }) })