pax_global_header00006660000000000000000000000064135024446560014523gustar00rootroot0000000000000052 comment=b73e204c7670a8f01428a8c220bcd337d395bdf6 p-finally-2.0.1/000077500000000000000000000000001350244465600134165ustar00rootroot00000000000000p-finally-2.0.1/.editorconfig000066400000000000000000000002571350244465600160770ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 p-finally-2.0.1/.gitattributes000066400000000000000000000000231350244465600163040ustar00rootroot00000000000000* text=auto eol=lf p-finally-2.0.1/.github/000077500000000000000000000000001350244465600147565ustar00rootroot00000000000000p-finally-2.0.1/.github/funding.yml000066400000000000000000000001631350244465600171330ustar00rootroot00000000000000github: sindresorhus open_collective: sindresorhus tidelift: npm/p-finally custom: https://sindresorhus.com/donate p-finally-2.0.1/.github/security.md000066400000000000000000000002631350244465600171500ustar00rootroot00000000000000# Security Policy To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. p-finally-2.0.1/.gitignore000066400000000000000000000000271350244465600154050ustar00rootroot00000000000000node_modules yarn.lock p-finally-2.0.1/.npmrc000066400000000000000000000000231350244465600145310ustar00rootroot00000000000000package-lock=false p-finally-2.0.1/.travis.yml000066400000000000000000000000651350244465600155300ustar00rootroot00000000000000language: node_js node_js: - '12' - '10' - '8' p-finally-2.0.1/index.js000066400000000000000000000003361350244465600150650ustar00rootroot00000000000000'use strict'; module.exports = async ( promise, onFinally = (() => {}) ) => { let value; try { value = await promise; } catch (error) { await onFinally(); throw error; } await onFinally(); return value; }; p-finally-2.0.1/license000066400000000000000000000021251350244465600147630ustar00rootroot00000000000000MIT License Copyright (c) Sindre Sorhus (sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. p-finally-2.0.1/package.json000066400000000000000000000012251350244465600157040ustar00rootroot00000000000000{ "name": "p-finally", "version": "2.0.1", "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" } } p-finally-2.0.1/readme.md000066400000000000000000000025351350244465600152020ustar00rootroot00000000000000# p-finally [![Build Status](https://travis-ci.org/sindresorhus/p-finally.svg?branch=master)](https://travis-ci.org/sindresorhus/p-finally) > [`Promise#finally()`](https://github.com/tc39/proposal-promise-finally) [ponyfill](https://ponyfill.com) - Invoked when the promise is settled regardless of outcome Useful for cleanup. ## Install ``` $ npm install p-finally ``` ## Usage ```js const pFinally = require('p-finally'); const directory = createTempDirectory(); (async () => { await pFinally(write(directory), () => { cleanup(directory); }); }); ``` ## API ### pFinally(promise, onFinally?) Returns a `Promise`. #### onFinally Type: `Function` Note: Throwing or returning a rejected promise will reject `promise` with the rejection reason. ## Related - [p-try](https://github.com/sindresorhus/p-try) - `Promise.try()` ponyfill - Starts a promise chain - [More…](https://github.com/sindresorhus/promise-fun) ---
Get professional support for this package with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.
p-finally-2.0.1/test.js000066400000000000000000000027171350244465600147420ustar00rootroot00000000000000import test from 'ava'; import pFinally from '.'; const fixture = Symbol('fixture'); const fixtureError = new Error('error'); test('does nothing when nothing is passed', async t => { t.is(await pFinally(Promise.resolve(fixture)), fixture); }); test('callback is called when promise is fulfilled', async t => { let isCalled = false; const value = await pFinally(Promise.resolve(fixture), () => { isCalled = true; }); t.is(value, fixture); t.true(isCalled); }); test('callback is called when promise is rejected', async t => { let isCalled = false; await pFinally(Promise.reject(fixtureError), () => { isCalled = true; }).catch(error => { t.is(error, fixtureError); }); t.true(isCalled); }); test('returning a rejected promise in the callback rejects the promise', async t => { await pFinally(Promise.resolve(fixture), () => Promise.reject(fixtureError)).then(() => { t.fail(); }, 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 pFinally(Promise.reject(new Error('original error')), () => Promise.reject(fixtureError)).catch(error => { 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(() => {}); });