Skip to content

Commit

Permalink
fix: Waiting for promises to resolve
Browse files Browse the repository at this point in the history
Updating wrapped callback to wait for longer-running promises, and returning result/error through context succeed/fail wrappers
  • Loading branch information
mrickard authored Jul 9, 2019
2 parents c3237b6 + 914704c commit a3233ea
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,22 @@ class IOpipeWrapperClass {
this.context,
this.callback
);
if (result && result.then && result.catch) {
return new Promise(() => this.callback(null, () => result))
.then(value => value)
.catch(err => {
this.sendReport(err, () => this.originalCallback(err));
return err;
});
if (
result &&
typeof result.then === 'function' &&
typeof result.catch === 'function'
) {
return new Promise(resolve => {
return result
.then(value => {
this.context.succeed(value);
return this.callback(null, () => resolve(value));
})
.catch(err => {
this.context.fail(err);
return this.callback(err);
});
});
}
return result;
} catch (err) {
Expand Down

0 comments on commit a3233ea

Please sign in to comment.