diff --git a/lib/main.js b/lib/main.js index 18b41a04..4d7b6014 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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 @@ -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) @@ -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) { @@ -822,20 +822,36 @@ 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) }) }) } @@ -843,30 +859,48 @@ Lambda.prototype.deploy = function (program) { // 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) }) }) }) diff --git a/test/main.js b/test/main.js index 985932d3..5cc0518a 100644 --- a/test/main.js +++ b/test/main.js @@ -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',