Comparing version
24
index.js
'use strict'; | ||
module.exports = (promise, onFinally) => { | ||
onFinally = onFinally || (() => {}); | ||
return promise.then( | ||
val => new Promise(resolve => { | ||
resolve(onFinally()); | ||
}).then(() => val), | ||
err => new Promise(resolve => { | ||
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; | ||
} | ||
}; |
{ | ||
"name": "p-finally", | ||
"version": "1.0.0", | ||
"description": "`Promise#finally()` ponyfill - Invoked when the promise is settled regardless of outcome", | ||
"license": "MIT", | ||
"repository": "sindresorhus/p-finally", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"promise", | ||
"finally", | ||
"handler", | ||
"function", | ||
"async", | ||
"await", | ||
"promises", | ||
"settled", | ||
"ponyfill", | ||
"polyfill", | ||
"shim", | ||
"bluebird" | ||
], | ||
"devDependencies": { | ||
"ava": "*", | ||
"xo": "*" | ||
}, | ||
"xo": { | ||
"esnext": true | ||
} | ||
"name": "p-finally", | ||
"version": "2.0.0", | ||
"description": "`Promise#finally()` ponyfill - Invoked when the promise is settled regardless of outcome", | ||
"license": "MIT", | ||
"repository": "sindresorhus/p-finally", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"promise", | ||
"finally", | ||
"handler", | ||
"function", | ||
"async", | ||
"await", | ||
"promises", | ||
"settled", | ||
"ponyfill", | ||
"polyfill", | ||
"shim", | ||
"bluebird" | ||
], | ||
"devDependencies": { | ||
"ava": "^1.4.1", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -11,3 +11,3 @@ # p-finally [](https://travis-ci.org/sindresorhus/p-finally) | ||
``` | ||
$ npm install --save p-finally | ||
$ npm install p-finally | ||
``` | ||
@@ -21,5 +21,9 @@ | ||
const dir = createTempDir(); | ||
const directory = createTempDir(); | ||
pFinally(write(dir), () => cleanup(dir)); | ||
(async () => { | ||
await pFinally(write(directory), () => { | ||
cleanup(directory); | ||
}); | ||
}); | ||
``` | ||
@@ -43,8 +47,3 @@ | ||
- [p-try](https://github.com/sindresorhus/p-try) - `Promise#try()` ponyfill - Starts a promise chain | ||
- [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) |
Sorry, the diff of this file is not supported yet
2918
-6.2%47
-2.08%