Repository: motdotla/node-lambda
Pull request #316 (2017-06-09 10:54:03)

Replace asyncjs of _updateEventSources with Promise
Related #300


Patch:
diff --git a/lib/main.js b/lib/main.js
index 45716de..808b7ea 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)
   })
 }
 


Test patch:
diff --git a/test/main.js b/test/main.js
index a34a235..54283be 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)
+      })
     })
   })