├── .editorconfig ├── .gitattributes ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .npmrc ├── index.d.ts ├── index.js ├── index.test-d.ts ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 18 14 | - 16 15 | - 14 16 | steps: 17 | - uses: actions/checkout@v4 18 | - uses: actions/setup-node@v4 19 | with: 20 | node-version: ${{ matrix.node-version }} 21 | - run: npm install 22 | - run: npm test 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export type Options = { 2 | /** 3 | Delay the rejection. 4 | 5 | Turn this off if you want a rejected promise to fail fast. 6 | 7 | @default true 8 | */ 9 | readonly delayRejection?: boolean; 10 | }; 11 | 12 | /** 13 | Delay a promise a minimum amount of time. 14 | 15 | @param promise - Promise to delay. 16 | @param minimumDelay - Time in milliseconds. 17 | 18 | @example 19 | ``` 20 | import pMinDelay from 'p-min-delay'; 21 | 22 | const value = await pMinDelay(somePromise, 1000); 23 | // Executed after minimum 1 second even if `somePromise` fulfills before that 24 | ``` 25 | */ 26 | export default function pMinDelay( 27 | promise: PromiseLike, 28 | minimumDelay: number, 29 | options?: Options 30 | ): Promise; 31 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import delay from 'yoctodelay'; 2 | 3 | export default async function pMinDelay(promise, minimumDelay, {delayRejection = true} = {}) { 4 | const delayPromise = delay(minimumDelay); 5 | await (delayRejection ? delayPromise : Promise.all([promise, delayPromise])); 6 | return promise; 7 | } 8 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import pMinDelay from './index.js'; 3 | 4 | expectType>(pMinDelay(Promise.resolve(1), 1000)); 5 | expectType>( 6 | pMinDelay(Promise.resolve('1'), 1000, {delayRejection: false}), 7 | ); 8 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "p-min-delay", 3 | "version": "4.1.0", 4 | "description": "Delay a promise a minimum amount of time", 5 | "license": "MIT", 6 | "repository": "sindresorhus/p-min-delay", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "types": "./index.d.ts", 16 | "sideEffects": false, 17 | "engines": { 18 | "node": ">=12" 19 | }, 20 | "scripts": { 21 | "test": "xo && ava && tsd" 22 | }, 23 | "files": [ 24 | "index.js", 25 | "index.d.ts" 26 | ], 27 | "keywords": [ 28 | "promise", 29 | "delay", 30 | "minimum", 31 | "min", 32 | "resolve", 33 | "stall", 34 | "defer", 35 | "wait", 36 | "timeout", 37 | "settimeout", 38 | "event", 39 | "loop", 40 | "next", 41 | "tick", 42 | "delay", 43 | "async", 44 | "await", 45 | "promises", 46 | "bluebird" 47 | ], 48 | "dependencies": { 49 | "yoctodelay": "^2.0.0" 50 | }, 51 | "devDependencies": { 52 | "ava": "^5.2.0", 53 | "in-range": "^3.0.0", 54 | "time-span": "^5.0.0", 55 | "tsd": "^0.28.1", 56 | "xo": "^0.54.1" 57 | }, 58 | "ava": { 59 | "serial": true 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # p-min-delay 2 | 3 | > Delay a promise a minimum amount of time 4 | 5 | While the [`delay`](https://github.com/sindresorhus/delay) module delays the promise a specified amount of time and then resolves it, this module ensures the promise resolves after the specified amount of time. 6 | 7 | Useful when you have a promise that may settle immediately or may take some time, and you want to ensure it doesn't settle too fast. For example, if you want to show a loading indicator for at least 1 second (but longer if needed) to prevent a confusing flash in the UI. 8 | 9 | ## Install 10 | 11 | ```sh 12 | npm install p-min-delay 13 | ``` 14 | 15 | ## Usage 16 | 17 | ```js 18 | import pMinDelay from 'p-min-delay'; 19 | 20 | const value = await pMinDelay(somePromise, 1000); 21 | // Executed after minimum 1 second even if `somePromise` fulfills before that 22 | ``` 23 | 24 | ## API 25 | 26 | ### pMinDelay(promise, minimumDelay, options?) 27 | 28 | #### promise 29 | 30 | Type: `Promise` 31 | 32 | Promise to delay. 33 | 34 | #### minimumDelay 35 | 36 | Type: `number` 37 | 38 | Time in milliseconds. 39 | 40 | #### options 41 | 42 | Type: `Object` 43 | 44 | ##### delayRejection 45 | 46 | Type: `boolean`\ 47 | Default: `true` 48 | 49 | Delay the rejection. 50 | 51 | Turn this off if you want a rejected promise to fail fast. 52 | 53 | ## Related 54 | 55 | - [delay](https://github.com/sindresorhus/delay) - Delay a promise a specified amount of time 56 | - [p-immediate](https://github.com/sindresorhus/p-immediate) - Returns a promise resolved in the next event loop - think `setImmediate()` 57 | - [p-timeout](https://github.com/sindresorhus/p-timeout) - Timeout a promise after a specified amount of time 58 | - [More…](https://github.com/sindresorhus/promise-fun) 59 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import delay from 'yoctodelay'; 3 | import timeSpan from 'time-span'; 4 | import inRange from 'in-range'; 5 | import pMinDelay from './index.js'; 6 | 7 | const fixture = Symbol('fixture'); 8 | 9 | test('only settles after minimum delay', async t => { 10 | const end = timeSpan(); 11 | const result = await pMinDelay(Promise.resolve(fixture), 200); 12 | t.is(result, fixture); 13 | t.true(inRange(end(), {start: 170, end: 230})); 14 | }); 15 | 16 | test('accept non-promise object', async t => { 17 | const end = timeSpan(); 18 | const result = await pMinDelay(fixture, 200); 19 | t.is(result, fixture); 20 | t.true(inRange(end(), {start: 170, end: 230})); 21 | }); 22 | 23 | test('promise takes longer than minimum delay', async t => { 24 | const end = timeSpan(); 25 | await pMinDelay(delay(200), 100); 26 | t.true(inRange(end(), {start: 170, end: 230})); 27 | }); 28 | 29 | test('minimum delay applies to rejection too', async t => { 30 | const end = timeSpan(); 31 | await pMinDelay(Promise.reject(), 100).catch(() => {}); 32 | t.true(inRange(end(), {start: 70, end: 130})); 33 | }); 34 | 35 | test('option - {delayRejection:false}', async t => { 36 | const end = timeSpan(); 37 | await pMinDelay(Promise.reject(), 100, {delayRejection: false}).catch(() => {}); 38 | t.true(inRange(end(), {start: 0, end: 30})); 39 | }); 40 | --------------------------------------------------------------------------------