Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace async.js of deploy with Promise #319

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 67 additions & 33 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ Lambda.prototype._listEventSourceMappings = function (lambda, params, cb) {

Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourceList, eventSourceList, cb) => {
if (eventSourceList == null) {
return cb(null, [])
return new Promise(resolve => cb(null, []))
}
const updateEventSourceList = []
// Checking new and update event sources
Expand Down Expand Up @@ -705,14 +705,14 @@ Lambda.prototype._updateEventSources = (lambda, functionName, existingEventSourc

Lambda.prototype._updateScheduleEvents = (scheduleEvents, functionArn, scheduleList, cb) => {
if (scheduleList == null) {
return cb(null, [])
return new Promise(resolve => cb(null, []))
}

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

// series
paramsList.map((params) => {
return paramsList.map((params) => {
return scheduleEvents.add(params)
}).reduce((a, b) => {
return a.then(b)
Expand Down Expand Up @@ -812,7 +812,7 @@ Lambda.prototype.deploy = function (program) {
// Checking function
return lambda.getFunction({
'FunctionName': params.FunctionName
}, function (err) {
}, (err) => {
if (err) {
// Function does not exist
return _this._uploadNew(lambda, params, function (err, results) {
Expand All @@ -822,51 +822,85 @@ Lambda.prototype.deploy = function (program) {
console.log('=> Zip file(s) done uploading. Results follow: ')
console.log(results)

async.parallel([
function (_callback) {
// Updating event source(s)
_this._updateEventSources(lambda, params.FunctionName, [], eventSourceList.EventSourceMappings, function (err, results) {
_callback(err, results)
})
},
function (_callback) {
_this._updateScheduleEvents(scheduleEvents, results.FunctionArn, eventSourceList.ScheduleEvents, function (err, results) {
_callback(err, results)
})
}
], function (err, results) {
cb(err, results)
// This code is on its way to Promise.
// From now on, callback will not be used.
return Promise.all([
new Promise((resolve, reject) => {
_this._updateEventSources(
lambda,
params.FunctionName,
[],
eventSourceList.EventSourceMappings,
(err, results) => {
if (err) return reject(err)
resolve(results)
}
)
}),
new Promise((resolve, reject) => {
_this._updateScheduleEvents(
scheduleEvents,
results.FunctionArn,
eventSourceList.ScheduleEvents,
(err, results) => {
if (err) return reject(err)
resolve(results)
}
)
})
]).then((results) => {
cb(null, results)
}).catch((err) => {
cb(err)
})
})
}

// Function exists
_this._listEventSourceMappings(lambda, {
'FunctionName': params.FunctionName
}, function (err, existingEventSourceList) {
}, (err, existingEventSourceList) => {
if (err) {
throw err
}
return async.parallel([
function (_callback) {
_this._uploadExisting(lambda, params, function (err, results) {

// This code is on its way to Promise.
// From now on, callback will not be used.
return Promise.all([
new Promise((resolve, reject) => {
_this._uploadExisting(lambda, params, (err, results) => {
if (err) {
throw err
}
console.log('=> Zip file(s) done uploading. Results follow: ')
console.log(results)
_this._updateScheduleEvents(scheduleEvents, results.FunctionArn, eventSourceList.ScheduleEvents, function (err, results) {
_callback(err, results)
})
})
},
function (_callback) {
_this._updateEventSources(lambda, params.FunctionName, existingEventSourceList, eventSourceList.EventSourceMappings, function (err, results) {
_callback(err, results)
_this._updateScheduleEvents(
scheduleEvents,
results.FunctionArn,
eventSourceList.ScheduleEvents,
(err, results) => {
if (err) return reject(err)
resolve(results)
}
)
})
}
], function (err, results) {
cb(err, results)
}),
new Promise((resolve, reject) => {
_this._updateEventSources(
lambda,
params.FunctionName,
existingEventSourceList,
eventSourceList.EventSourceMappings,
(err, results) => {
if (err) return reject(err)
resolve(results)
}
)
})
]).then((results) => {
cb(null, results)
}).catch((err) => {
cb(err)
})
})
})
Expand Down
1 change: 1 addition & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const lambda = require(path.join(__dirname, '..', 'lib', 'main'))
const Zip = require('node-zip')
const assert = require('chai').assert
const awsMock = require('aws-sdk-mock')
awsMock.setSDK(path.resolve('node_modules/aws-sdk'))

const originalProgram = {
environment: 'development',
Expand Down