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

Commit

Permalink
Require Node.js 8
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 28, 2019
1 parent 020c2cd commit e9250d1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 41 deletions.
3 changes: 1 addition & 2 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
* text=auto
*.js text eol=lf
* text=auto eol=lf
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js
node_js:
- '12'
- '10'
- '8'
- '6'
- '4'
20 changes: 12 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
'use strict';
module.exports = (promise, onFinally) => {
onFinally = onFinally || (() => {});

return promise.then(
val => Promise.resolve(onFinally()).then(() => val),
err => Promise.resolve(onFinally()).then(() => {
throw err;
})
);
module.exports = async (
promise,
onFinally = (() => {})
) => {
try {
const value = await promise;
await onFinally();
return value;
} catch (error) {
await onFinally();
throw error;
}
};
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
"node": ">=8"
},
"scripts": {
"test": "xo && ava"
Expand All @@ -33,7 +33,7 @@
"bluebird"
],
"devDependencies": {
"ava": "*",
"xo": "*"
"ava": "^1.4.1",
"xo": "^0.24.0"
}
}
13 changes: 6 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ $ npm install p-finally
```js
const pFinally = require('p-finally');

const dir = createTempDir();
const directory = createTempDir();

pFinally(write(dir), () => cleanup(dir));
(async () => {
await pFinally(write(directory), () => {
cleanup(directory);
});
});
```


Expand All @@ -40,8 +44,3 @@ Note: Throwing or returning a rejected promise will reject `promise` with the re

- [p-try](https://github.com/sindresorhus/p-try) - `Promise.try()` ponyfill - Starts a promise chain
- [More…](https://github.com/sindresorhus/promise-fun)


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
38 changes: 19 additions & 19 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
import test from 'ava';
import m from '.';
import pFinally from '.';

const fixture = Symbol('fixture');
const fixtureErr = new Error('err');
const fixtureError = new Error('error');

test('does nothing when nothing is passed', async t => {
t.is(await m(Promise.resolve(fixture)), fixture);
t.is(await pFinally(Promise.resolve(fixture)), fixture);
});

test('callback is called when promise is fulfilled', async t => {
let called = false;
let isCalled = false;

const val = await m(Promise.resolve(fixture), () => {
called = true;
const value = await pFinally(Promise.resolve(fixture), () => {
isCalled = true;
});

t.is(val, fixture);
t.true(called);
t.is(value, fixture);
t.true(isCalled);
});

test('callback is called when promise is rejected', async t => {
let called = false;
let isCalled = false;

await m(Promise.reject(fixtureErr), () => {
called = true;
}).catch(err => {
t.is(err, fixtureErr);
await pFinally(Promise.reject(fixtureError), () => {
isCalled = true;
}).catch(error => {
t.is(error, fixtureError);
});

t.true(called);
t.true(isCalled);
});

test('returning a rejected promise in the callback rejects the promise', async t => {
await m(Promise.resolve(fixture), () => Promise.reject(fixtureErr)).then(() => {
await pFinally(Promise.resolve(fixture), () => Promise.reject(fixtureError)).then(() => {
t.fail();
}, err => {
t.is(err, fixtureErr);
}, error => {
t.is(error, fixtureError);
});
});

test('returning a rejected promise in the callback for an already rejected promise changes the rejection reason', async t => {
await m(Promise.reject(new Error('orig err')), () => Promise.reject(fixtureErr)).catch(err => {
t.is(err, fixtureErr);
await pFinally(Promise.reject(new Error('original error')), () => Promise.reject(fixtureError)).catch(error => {
t.is(error, fixtureError);
});
});

0 comments on commit e9250d1

Please sign in to comment.