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 async callback handling (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
galbash committed Dec 6, 2018
1 parent f024be3 commit 2452110
Showing 1 changed file with 21 additions and 7 deletions.
28 changes: 21 additions & 7 deletions src/wrappers/lambda.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function baseLambdaWrapper(
if (callbackCalled) {
return Promise.resolve();
}
callbackCalled = true;
if (error) { // not catching false here, but that seems OK
eventInterface.setException(runner, error);
}
Expand Down Expand Up @@ -117,15 +118,17 @@ function baseLambdaWrapper(
return tracer.sendTraceSync();
};

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


try {
runner.setStartTime(utils.createTimestampFromTime(startTime));
let result = (
Expand All @@ -135,12 +138,23 @@ function baseLambdaWrapper(
);

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

0 comments on commit 2452110

Please sign in to comment.