Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ For notes on migrating to 2.x / 0.200.x see [the upgrade guide](doc/upgrade-to-2

### :rocket: Features

* feat(instrumentation): allow error of safeExecuteInTheMiddleAsync to be async [#6032](https://github.com/open-telemetry/opentelemetry-js/pull/6032) @JPeer264

### :bug: Bug Fixes

### :books: Documentation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ export function safeExecuteInTheMiddle<T>(
*/
export async function safeExecuteInTheMiddleAsync<T>(
execute: () => T,
onFinish: (e: Error | undefined, result: T | undefined) => void,
onFinish: (
e: Error | undefined,
result: T | undefined
) => Promise<void> | void,
preventThrowingError?: boolean
): Promise<T> {
let error: Error | undefined;
Expand All @@ -60,7 +63,7 @@ export async function safeExecuteInTheMiddleAsync<T>(
} catch (e) {
error = e;
} finally {
onFinish(error, result);
await onFinish(error, result);
if (error && !preventThrowingError) {
// eslint-disable-next-line no-unsafe-finally
throw error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,15 @@ describe('safeExecuteInTheMiddle', function () {
});

describe('safeExecuteInTheMiddleAsync', function () {
it('should not throw error', function () {
it('should not throw error', function (done) {
safeExecuteInTheMiddleAsync(
async () => {
await setTimeout(() => {}, 1);
await new Promise(res => setTimeout(res, 1));
return 'foo';
},
err => {
assert.deepStrictEqual(err, undefined);
done();
},
true
);
Expand All @@ -106,7 +107,7 @@ describe('safeExecuteInTheMiddleAsync', function () {
try {
await safeExecuteInTheMiddleAsync(
async () => {
await setTimeout(() => {}, 1);
await new Promise(res => setTimeout(res, 1));
throw error;
},
err => {
Expand All @@ -120,7 +121,7 @@ describe('safeExecuteInTheMiddleAsync', function () {
it('should return result', async function () {
const result = await safeExecuteInTheMiddleAsync(
async () => {
await setTimeout(() => {}, 1);
await new Promise(res => setTimeout(res, 1));
return 1;
},
(err, result) => {
Expand All @@ -130,4 +131,17 @@ describe('safeExecuteInTheMiddleAsync', function () {
);
assert.deepStrictEqual(result, 1);
});
it('should wait for the error', async function () {
const result = await Promise.race([
safeExecuteInTheMiddleAsync(
() => 1,
async () => {
await new Promise(res => setTimeout(res, 100));
}
),
new Promise(res => setTimeout(() => res('waited'), 10)),
]);

assert.deepStrictEqual(result, 'waited');
});
});