Skip to content

Commit 4e8285a

Browse files
abetomoDeviaVir
authored andcommitted
Modified the function used in deploy to return Promise (#320)
* Replace async with Promise * Modify to arrow function * Modify from var to const * Modify _uploadExisting returns Promise * Modify _uploadNew returns Promise * Space after "throw" * Modify _updateScheduleEvents returns Promise * Modify _updateEventSources returns Promise * Remove unnecessary arguments
1 parent 10a0557 commit 4e8285a

File tree

2 files changed

+123
-184
lines changed

2 files changed

+123
-184
lines changed

lib/main.js

Lines changed: 76 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -464,49 +464,51 @@ Lambda.prototype._setRunTimeEnvironmentVars = function (program) {
464464
}
465465
}
466466

467-
Lambda.prototype._uploadExisting = (lambda, params, cb) => {
468-
const request = lambda.updateFunctionCode({
469-
'FunctionName': params.FunctionName,
470-
'ZipFile': params.Code.ZipFile,
471-
'Publish': params.Publish
472-
}, (err, data) => {
473-
if (err) {
474-
return cb(err, data)
475-
}
476-
477-
return lambda.updateFunctionConfiguration({
467+
Lambda.prototype._uploadExisting = (lambda, params) => {
468+
return new Promise((resolve, reject) => {
469+
const request = lambda.updateFunctionCode({
478470
'FunctionName': params.FunctionName,
479-
'Description': params.Description,
480-
'Handler': params.Handler,
481-
'MemorySize': params.MemorySize,
482-
'Role': params.Role,
483-
'Timeout': params.Timeout,
484-
'Runtime': params.Runtime,
485-
'VpcConfig': params.VpcConfig,
486-
'Environment': params.Environment,
487-
'DeadLetterConfig': params.DeadLetterConfig,
488-
'TracingConfig': params.TracingConfig
489-
}, (err, data) => {
490-
return cb(err, data)
471+
'ZipFile': params.Code.ZipFile,
472+
'Publish': params.Publish
473+
}, (err) => {
474+
if (err) return reject(err)
475+
476+
lambda.updateFunctionConfiguration({
477+
'FunctionName': params.FunctionName,
478+
'Description': params.Description,
479+
'Handler': params.Handler,
480+
'MemorySize': params.MemorySize,
481+
'Role': params.Role,
482+
'Timeout': params.Timeout,
483+
'Runtime': params.Runtime,
484+
'VpcConfig': params.VpcConfig,
485+
'Environment': params.Environment,
486+
'DeadLetterConfig': params.DeadLetterConfig,
487+
'TracingConfig': params.TracingConfig
488+
}, (err, data) => {
489+
if (err) return reject(err)
490+
resolve(data)
491+
})
491492
})
492-
})
493493

494-
request.on('retry', (response) => {
495-
console.log(response.error.message)
496-
console.log('=> Retrying')
494+
request.on('retry', (response) => {
495+
console.log(response.error.message)
496+
console.log('=> Retrying')
497+
})
497498
})
498-
499-
return request
500499
}
501500

502-
Lambda.prototype._uploadNew = (lambda, params, cb) => {
503-
const request = lambda.createFunction(params, (err, data) => cb(err, data))
504-
request.on('retry', (response) => {
505-
console.log(response.error.message)
506-
console.log('=> Retrying')
501+
Lambda.prototype._uploadNew = (lambda, params) => {
502+
return new Promise((resolve, reject) => {
503+
const request = lambda.createFunction(params, (err, data) => {
504+
if (err) return reject(err)
505+
resolve(data)
506+
})
507+
request.on('retry', (response) => {
508+
console.log(response.error.message)
509+
console.log('=> Retrying')
510+
})
507511
})
508-
509-
return request
510512
}
511513

512514
Lambda.prototype._readArchive = function (program, archiveCallback) {
@@ -614,9 +616,9 @@ Lambda.prototype._listEventSourceMappings = function (lambda, params, cb) {
614616
})
615617
}
616618

617-
Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourceList, eventSourceList, cb) => {
619+
Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourceList, eventSourceList) => {
618620
if (eventSourceList == null) {
619-
return new Promise(resolve => cb(null, []))
621+
return Promise.resolve([])
620622
}
621623
const updateEventSourceList = []
622624
// Checking new and update event sources
@@ -697,15 +699,15 @@ Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourc
697699
}
698700
return Promise.resolve()
699701
})).then((data) => {
700-
cb(null, data)
702+
return Promise.resolve(data)
701703
}).catch((err) => {
702-
cb(err)
704+
return Promise.reject(err)
703705
})
704706
}
705707

706-
Lambda.prototype._updateScheduleEvents = (scheduleEvents, functionArn, scheduleList, cb) => {
708+
Lambda.prototype._updateScheduleEvents = (scheduleEvents, functionArn, scheduleList) => {
707709
if (scheduleList == null) {
708-
return new Promise(resolve => cb(null, []))
710+
return Promise.resolve([])
709711
}
710712

711713
const paramsList = scheduleList.map((schedule) =>
@@ -720,9 +722,9 @@ Lambda.prototype._updateScheduleEvents = (scheduleEvents, functionArn, scheduleL
720722
// Since `scheduleEvents.add(params)` returns only `{}` if it succeeds
721723
// it is not very meaningful.
722724
// Therefore, return the params used for execution
723-
cb(null, paramsList)
725+
return Promise.resolve(paramsList)
724726
}).catch((err) => {
725-
cb(err)
727+
return Promise.reject(err)
726728
})
727729
}
728730

@@ -815,44 +817,31 @@ Lambda.prototype.deploy = function (program) {
815817
}, (err) => {
816818
if (err) {
817819
// Function does not exist
818-
return _this._uploadNew(lambda, params, function (err, results) {
819-
if (err) {
820-
throw err
821-
}
820+
return _this._uploadNew(lambda, params).then((results) => {
822821
console.log('=> Zip file(s) done uploading. Results follow: ')
823822
console.log(results)
824823

825824
// This code is on its way to Promise.
826825
// From now on, callback will not be used.
827826
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-
})
827+
_this._updateEventSources(
828+
lambda,
829+
params.FunctionName,
830+
[],
831+
eventSourceList.EventSourceMappings
832+
),
833+
_this._updateScheduleEvents(
834+
scheduleEvents,
835+
results.FunctionArn,
836+
eventSourceList.ScheduleEvents
837+
)
851838
]).then((results) => {
852839
cb(null, results)
853840
}).catch((err) => {
854841
cb(err)
855842
})
843+
}).catch((err) => {
844+
return Promise.reject(err)
856845
})
857846
}
858847

@@ -867,36 +856,23 @@ Lambda.prototype.deploy = function (program) {
867856
// This code is on its way to Promise.
868857
// From now on, callback will not be used.
869858
return Promise.all([
870-
new Promise((resolve, reject) => {
871-
_this._uploadExisting(lambda, params, (err, results) => {
872-
if (err) {
873-
throw err
874-
}
875-
console.log('=> Zip file(s) done uploading. Results follow: ')
876-
console.log(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-
)
886-
})
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-
}
859+
_this._uploadExisting(lambda, params).then((results) => {
860+
console.log('=> Zip file(s) done uploading. Results follow: ')
861+
console.log(results)
862+
return _this._updateScheduleEvents(
863+
scheduleEvents,
864+
results.FunctionArn,
865+
eventSourceList.ScheduleEvents
898866
)
899-
})
867+
}).catch((err) => {
868+
return Promise.reject(err)
869+
}),
870+
_this._updateEventSources(
871+
lambda,
872+
params.FunctionName,
873+
existingEventSourceList,
874+
eventSourceList.EventSourceMappings
875+
)
900876
]).then((results) => {
901877
cb(null, results)
902878
}).catch((err) => {

0 commit comments

Comments
 (0)