Skip to content

Commit 5de32fb

Browse files
abetomoDeviaVir
authored andcommitted
Refactoring _listEventSourceMappings (#339)
* Refactoring _listEventSourceMappings Modify that _listEventSourceMappings returns Promise * Remove unnecessary catch
1 parent 401a5d5 commit 5de32fb

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

lib/main.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -566,13 +566,15 @@ Lambda.prototype._buildAndArchive = function (program) {
566566
})
567567
}
568568

569-
Lambda.prototype._listEventSourceMappings = function (lambda, params, cb) {
570-
return lambda.listEventSourceMappings(params, function (err, data) {
571-
var eventSourceMappings = []
572-
if (!err && data && data.EventSourceMappings) {
573-
eventSourceMappings = data.EventSourceMappings
574-
}
575-
return cb(err, eventSourceMappings)
569+
Lambda.prototype._listEventSourceMappings = (lambda, params) => {
570+
return new Promise((resolve, reject) => {
571+
lambda.listEventSourceMappings(params, (err, data) => {
572+
if (err) return reject(err)
573+
if (data && data.EventSourceMappings) {
574+
return resolve(data.EventSourceMappings)
575+
}
576+
return resolve([])
577+
})
576578
})
577579
}
578580

@@ -764,11 +766,9 @@ Lambda.prototype._deployToRegion = function (program, params, region) {
764766
'FunctionName': params.FunctionName
765767
}).promise().then(() => {
766768
// Function exists
767-
_this._listEventSourceMappings(lambda, {
769+
return _this._listEventSourceMappings(lambda, {
768770
'FunctionName': params.FunctionName
769-
}, (err, existingEventSourceList) => {
770-
if (err) return reject(err)
771-
771+
}).then((existingEventSourceList) => {
772772
return Promise.all([
773773
_this._uploadExisting(lambda, params).then((results) => {
774774
console.log('=> Zip file(s) done uploading. Results follow: ')
@@ -787,9 +787,9 @@ Lambda.prototype._deployToRegion = function (program, params, region) {
787787
)
788788
]).then((results) => {
789789
resolve(results)
790-
}).catch((err) => {
791-
reject(err)
792790
})
791+
}).catch((err) => {
792+
reject(err)
793793
})
794794
}).catch(() => {
795795
// Function does not exist
@@ -811,8 +811,6 @@ Lambda.prototype._deployToRegion = function (program, params, region) {
811811
)
812812
]).then((results) => {
813813
resolve(results)
814-
}).catch((err) => {
815-
reject(err)
816814
})
817815
}).catch((err) => {
818816
reject(err)

test/main.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -790,6 +790,20 @@ describe('lib/main', function () {
790790
})
791791
})
792792

793+
describe('_listEventSourceMappings', () => {
794+
it('simple test with mock', () => {
795+
return lambda._listEventSourceMappings(
796+
awsLambda,
797+
{ FunctionName: 'test-func' }
798+
).then((results) => {
799+
assert.deepEqual(
800+
results,
801+
lambdaMockSettings.listEventSourceMappings.EventSourceMappings
802+
)
803+
})
804+
})
805+
})
806+
793807
describe('_updateEventSources', () => {
794808
const eventSourcesJsonValue = {
795809
EventSourceMappings: [{

0 commit comments

Comments
 (0)