Skip to content
This repository has been archived by the owner on Apr 9, 2021. It is now read-only.

Commit

Permalink
Fix the onFinally callback being called twice in some cases
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
sindresorhus committed Jun 19, 2019
1 parent f0e84e7 commit 56e29ef
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
8 changes: 5 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ module.exports = async (
promise,
onFinally = (() => {})
) => {
let value;
try {
const value = await promise;
await onFinally();
return value;
value = await promise;
} catch (error) {
await onFinally();
throw error;
}

await onFinally();
return value;
};
12 changes: 12 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,15 @@ test('returning a rejected promise in the callback for an already rejected promi
t.is(error, fixtureError);
});
});

test('the onFinally callback is only called once no matter what', async t => {
t.plan(1);

await pFinally(
Promise.resolve(),
() => {
t.pass();
throw new Error(); // eslint-disable-line unicorn/error-message
}
).catch(() => {});
});

0 comments on commit 56e29ef

Please sign in to comment.