From 32b8db9a573814ce07ad25ae1db96ead56d0c700 Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 9 Jun 2017 19:18:05 +0900 Subject: [PATCH 1/3] Replace async with Promise --- lib/main.js | 33 ++++++++++++++-------- test/main.js | 78 ++++++++++++++++++++++++++++++---------------------- 2 files changed, 66 insertions(+), 45 deletions(-) diff --git a/lib/main.js b/lib/main.js index 45716deb..c1b9e41e 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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) + }) }) }) From 99641807bb6bd12a0c1c2fb0e0577ac2bb43dc02 Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 9 Jun 2017 19:19:20 +0900 Subject: [PATCH 2/3] Modify to arrow function --- lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/main.js b/lib/main.js index c1b9e41e..0a82310c 100644 --- a/lib/main.js +++ b/lib/main.js @@ -614,7 +614,7 @@ 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, []) } From bdacf9766aca94fd42cb6e24126dc48eef4bf2be Mon Sep 17 00:00:00 2001 From: abetomo Date: Fri, 9 Jun 2017 19:19:34 +0900 Subject: [PATCH 3/3] Modify from var to const --- lib/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/main.js b/lib/main.js index 0a82310c..808b7ea5 100644 --- a/lib/main.js +++ b/lib/main.js @@ -618,7 +618,7 @@ Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourc if (eventSourceList == null) { return cb(null, []) } - var updateEventSourceList = [] + const updateEventSourceList = [] // Checking new and update event sources for (let i in eventSourceList) { let isExisting = false