Skip to content

Commit c37577a

Browse files
committed
Support async lambda functions
1 parent e6879f2 commit c37577a

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

lib/runner.js

+14-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,21 @@ process.on('message', (m) => {
2626
get: invokedFunctionArn
2727
});
2828
setGetRemainingTimeInMillis(context);
29-
lambda_module.handle(m.event, context, (err, data) => {
29+
30+
function handleResponse(err, data) {
3031
const res = data ? JSON.stringify(data) : null;
3132
const mem = process.memoryUsage();
3233
process.send({ id: m.id, err, res, mem });
33-
});
34+
}
35+
36+
if (lambda_module.handle.length < 3) {
37+
lambda_module
38+
.handle(m.event, context)
39+
.then((data) => {
40+
handleResponse(null, data);
41+
})
42+
.catch(handleResponse);
43+
} else {
44+
lambda_module.handle(m.event, context, handleResponse);
45+
}
3446
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
'use strict';
2+
3+
exports.handle = function (event) {
4+
return new Promise((resolve) => {
5+
setTimeout(() => {
6+
resolve(`Hello ${event.name}`);
7+
}, 1);
8+
});
9+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
exports.handle = function () {
4+
throw new Error('Oh noes!');
5+
};

test/fixture/functions/throw/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use strict';
22

3-
exports.handle = function () {
3+
exports.handle = function (_event, _context, _callback) {
44
throw new Error('Oh noes!');
55
};

test/lambda-test.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,20 @@ describe('lambda', () => {
3232
});
3333
});
3434

35+
it('invokes async lambda', (done) => {
36+
lambda = Lambda.create();
37+
38+
lambda.invoke(
39+
'hello-async',
40+
{ name: 'JavaScript Studio' },
41+
(err, value) => {
42+
assert.isNull(err);
43+
assert.equals(value, 'Hello JavaScript Studio');
44+
done();
45+
}
46+
);
47+
});
48+
3549
it('handles invalid response', (done) => {
3650
lambda = Lambda.create();
3751

@@ -513,7 +527,7 @@ describe('lambda', () => {
513527
});
514528
});
515529

516-
it('handled dying lambda', (done) => {
530+
it('handles throwing lambda', (done) => {
517531
sinon.stub(process.stderr, 'write');
518532

519533
lambda = Lambda.create();
@@ -524,6 +538,17 @@ describe('lambda', () => {
524538
});
525539
});
526540

541+
it('handles throwing async lambda', (done) => {
542+
sinon.stub(process.stderr, 'write');
543+
544+
lambda = Lambda.create();
545+
546+
lambda.invoke('throw-async', {}, (err) => {
547+
assert.json(err, { code: 'ERR_FAILED' });
548+
done();
549+
});
550+
});
551+
527552
it('runs node process with default memory', (done) => {
528553
lambda = Lambda.create({});
529554

0 commit comments

Comments
 (0)