Skip to content
This repository has been archived by the owner on Jun 13, 2023. It is now read-only.

Commit

Permalink
fix(lambda_wrapper): fixing wrapping async functions which call callb…
Browse files Browse the repository at this point in the history
…ack (#17)
  • Loading branch information
galbash authored and ranrib committed Dec 6, 2018
1 parent 14aaaf6 commit f024be3
Showing 1 changed file with 16 additions and 9 deletions.
25 changes: 16 additions & 9 deletions src/wrappers/lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ function baseLambdaWrapper(
};

const handleUserExecutionDone = (error, result) => {
if (callbackCalled) {
return Promise.resolve();
}
if (error) { // not catching false here, but that seems OK
eventInterface.setException(runner, error);
}
Expand Down Expand Up @@ -115,10 +118,11 @@ function baseLambdaWrapper(
};

const wrappedCallback = (error, result) => {
handleUserExecutionDone(error, result);
utils.debugLog('calling User\'s callback');
handleUserExecutionDone(error, result).then(() => {
utils.debugLog('calling User\'s callback');
return originalCallback(error, result);
});
callbackCalled = true;
return originalCallback(error, result);
};


Expand All @@ -129,12 +133,15 @@ function baseLambdaWrapper(
functionToWrap(originalEvent, originalContext, wrappedCallback, runner) :
functionToWrap(originalEvent, originalContext, wrappedCallback)
);
if (!callbackCalled) {
if (result instanceof Promise) {
result = result
.then((res) => { handleUserExecutionDone(null, res); })
.catch((err) => { handleUserExecutionDone(err); });
}

if (result instanceof Promise) {
result = result
.then((res) => {
handleUserExecutionDone(null, res).then(() => res);
})
.catch((err) => {
handleUserExecutionDone(err).then(() => err);
});
}
return result;
} catch (err) {
Expand Down

0 comments on commit f024be3

Please sign in to comment.