Skip to content

Commit

Permalink
fix(context): support promise payloads to context succeed/fail
Browse files Browse the repository at this point in the history
Refactoring succeed/fail to handle promise payloads. Merge pull request #350 from mrickard/issue/212-support-succeed-promise
  • Loading branch information
mrickard authored Sep 25, 2019
2 parents 3e872ca + fa8d8d3 commit 9640024
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,13 +224,43 @@ class IOpipeWrapperClass {
}
}
}
succeed(data) {
this.sendReport(null, () => {
async succeed(data) {
const context = this;
if (data && typeof data.resolve === 'function') {
await data.resolve();
} else if (data && typeof data.then === 'function') {
return new Promise(resolve => {
return data
.then(value => {
context.sendReport(null, () => {
// eslint-disable-next-line no-undef
context.originalContext.succeed(data);
resolve(value);
});
})
.catch(err => context.fail(err));
});
}
return this.sendReport(null, () => {
this.originalContext.succeed(data);
});
}
fail(err) {
this.sendReport(err, () => {
async fail(err) {
const context = this;
const handleErr = payload =>
this.sendReport(payload, () => context.originalContext.fail(payload));

if (err && typeof err.resolve === 'function') {
await err.resolve();
} else if (typeof err.then === 'function') {
return new Promise(() => {
return err
.then(value => handleErr(value))
.catch(errErr => handleErr(errErr));
});
}

return this.sendReport(err, () => {
this.originalContext.fail(err);
});
}
Expand Down

0 comments on commit 9640024

Please sign in to comment.