Skip to content

Commit a3233ea

Browse files
authored
fix: Waiting for promises to resolve
Updating wrapped callback to wait for longer-running promises, and returning result/error through context succeed/fail wrappers
2 parents c3237b6 + 914704c commit a3233ea

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/index.js

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -156,13 +156,22 @@ class IOpipeWrapperClass {
156156
this.context,
157157
this.callback
158158
);
159-
if (result && result.then && result.catch) {
160-
return new Promise(() => this.callback(null, () => result))
161-
.then(value => value)
162-
.catch(err => {
163-
this.sendReport(err, () => this.originalCallback(err));
164-
return err;
165-
});
159+
if (
160+
result &&
161+
typeof result.then === 'function' &&
162+
typeof result.catch === 'function'
163+
) {
164+
return new Promise(resolve => {
165+
return result
166+
.then(value => {
167+
this.context.succeed(value);
168+
return this.callback(null, () => resolve(value));
169+
})
170+
.catch(err => {
171+
this.context.fail(err);
172+
return this.callback(err);
173+
});
174+
});
166175
}
167176
return result;
168177
} catch (err) {

0 commit comments

Comments
 (0)