Skip to content

Commit 10a0557

Browse files
abetomoDeviaVir
authored andcommitted
Replace async.js of deploy with Promise (#319)
* Fix to add awsMock.setSDK Because `npm test test/main.js` fails * Fix to return Promise * Replace async.parallel with Promise.all * Replace async.parallel with Promise.all
1 parent d43c685 commit 10a0557

File tree

2 files changed

+68
-33
lines changed

2 files changed

+68
-33
lines changed

lib/main.js

Lines changed: 67 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ Lambda.prototype._listEventSourceMappings = function (lambda, params, cb) {
616616

617617
Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourceList, eventSourceList, cb) => {
618618
if (eventSourceList == null) {
619-
return cb(null, [])
619+
return new Promise(resolve => cb(null, []))
620620
}
621621
const updateEventSourceList = []
622622
// Checking new and update event sources
@@ -705,14 +705,14 @@ Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourc
705705

706706
Lambda.prototype._updateScheduleEvents = (scheduleEvents, functionArn, scheduleList, cb) => {
707707
if (scheduleList == null) {
708-
return cb(null, [])
708+
return new Promise(resolve => cb(null, []))
709709
}
710710

711711
const paramsList = scheduleList.map((schedule) =>
712712
Object.assign(schedule, { FunctionArn: functionArn }))
713713

714714
// series
715-
paramsList.map((params) => {
715+
return paramsList.map((params) => {
716716
return scheduleEvents.add(params)
717717
}).reduce((a, b) => {
718718
return a.then(b)
@@ -812,7 +812,7 @@ Lambda.prototype.deploy = function (program) {
812812
// Checking function
813813
return lambda.getFunction({
814814
'FunctionName': params.FunctionName
815-
}, function (err) {
815+
}, (err) => {
816816
if (err) {
817817
// Function does not exist
818818
return _this._uploadNew(lambda, params, function (err, results) {
@@ -822,51 +822,85 @@ Lambda.prototype.deploy = function (program) {
822822
console.log('=> Zip file(s) done uploading. Results follow: ')
823823
console.log(results)
824824

825-
async.parallel([
826-
function (_callback) {
827-
// Updating event source(s)
828-
_this._updateEventSources(lambda, params.FunctionName, [], eventSourceList.EventSourceMappings, function (err, results) {
829-
_callback(err, results)
830-
})
831-
},
832-
function (_callback) {
833-
_this._updateScheduleEvents(scheduleEvents, results.FunctionArn, eventSourceList.ScheduleEvents, function (err, results) {
834-
_callback(err, results)
835-
})
836-
}
837-
], function (err, results) {
838-
cb(err, results)
825+
// This code is on its way to Promise.
826+
// From now on, callback will not be used.
827+
return Promise.all([
828+
new Promise((resolve, reject) => {
829+
_this._updateEventSources(
830+
lambda,
831+
params.FunctionName,
832+
[],
833+
eventSourceList.EventSourceMappings,
834+
(err, results) => {
835+
if (err) return reject(err)
836+
resolve(results)
837+
}
838+
)
839+
}),
840+
new Promise((resolve, reject) => {
841+
_this._updateScheduleEvents(
842+
scheduleEvents,
843+
results.FunctionArn,
844+
eventSourceList.ScheduleEvents,
845+
(err, results) => {
846+
if (err) return reject(err)
847+
resolve(results)
848+
}
849+
)
850+
})
851+
]).then((results) => {
852+
cb(null, results)
853+
}).catch((err) => {
854+
cb(err)
839855
})
840856
})
841857
}
842858

843859
// Function exists
844860
_this._listEventSourceMappings(lambda, {
845861
'FunctionName': params.FunctionName
846-
}, function (err, existingEventSourceList) {
862+
}, (err, existingEventSourceList) => {
847863
if (err) {
848864
throw err
849865
}
850-
return async.parallel([
851-
function (_callback) {
852-
_this._uploadExisting(lambda, params, function (err, results) {
866+
867+
// This code is on its way to Promise.
868+
// From now on, callback will not be used.
869+
return Promise.all([
870+
new Promise((resolve, reject) => {
871+
_this._uploadExisting(lambda, params, (err, results) => {
853872
if (err) {
854873
throw err
855874
}
856875
console.log('=> Zip file(s) done uploading. Results follow: ')
857876
console.log(results)
858-
_this._updateScheduleEvents(scheduleEvents, results.FunctionArn, eventSourceList.ScheduleEvents, function (err, results) {
859-
_callback(err, results)
860-
})
861-
})
862-
},
863-
function (_callback) {
864-
_this._updateEventSources(lambda, params.FunctionName, existingEventSourceList, eventSourceList.EventSourceMappings, function (err, results) {
865-
_callback(err, results)
877+
_this._updateScheduleEvents(
878+
scheduleEvents,
879+
results.FunctionArn,
880+
eventSourceList.ScheduleEvents,
881+
(err, results) => {
882+
if (err) return reject(err)
883+
resolve(results)
884+
}
885+
)
866886
})
867-
}
868-
], function (err, results) {
869-
cb(err, results)
887+
}),
888+
new Promise((resolve, reject) => {
889+
_this._updateEventSources(
890+
lambda,
891+
params.FunctionName,
892+
existingEventSourceList,
893+
eventSourceList.EventSourceMappings,
894+
(err, results) => {
895+
if (err) return reject(err)
896+
resolve(results)
897+
}
898+
)
899+
})
900+
]).then((results) => {
901+
cb(null, results)
902+
}).catch((err) => {
903+
cb(err)
870904
})
871905
})
872906
})

test/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const lambda = require(path.join(__dirname, '..', 'lib', 'main'))
77
const Zip = require('node-zip')
88
const assert = require('chai').assert
99
const awsMock = require('aws-sdk-mock')
10+
awsMock.setSDK(path.resolve('node_modules/aws-sdk'))
1011

1112
const originalProgram = {
1213
environment: 'development',

0 commit comments

Comments
 (0)