Require Node.js 6, add TypeScript definition (#2)

This commit is contained in:
Dimitri Benin 2019-03-17 12:21:07 +00:00 committed by Sindre Sorhus
parent f3a74a0da0
commit 3510168005
12 changed files with 96 additions and 65 deletions

View File

@ -7,6 +7,6 @@ charset = utf-8
trim_trailing_whitespace = true trim_trailing_whitespace = true
insert_final_newline = true insert_final_newline = true
[{package.json,*.yml}] [*.yml]
indent_style = space indent_style = space
indent_size = 2 indent_size = 2

3
.gitattributes vendored
View File

@ -1,2 +1 @@
* text=auto * text=auto eol=lf
*.js text eol=lf

1
.gitignore vendored
View File

@ -1 +1,2 @@
node_modules node_modules
yarn.lock

1
.npmrc Normal file
View File

@ -0,0 +1 @@
package-lock=false

View File

@ -1,4 +1,5 @@
language: node_js language: node_js
node_js: node_js:
- '10'
- '8'
- '6' - '6'
- '4'

27
index.d.ts vendored Normal file
View File

@ -0,0 +1,27 @@
export interface DeferredPromise<ValueType> {
/**
Resolves the promise with a value or the result of another promise.
@param value - The value to resolve the promise with.
*/
resolve(value: ValueType | PromiseLike<ValueType>): void;
/**
Reject the promise with a provided reason or error.
@param reason - The reason or error to reject the promise with.
*/
reject(reason: unknown): void;
/**
The deferred promise.
*/
promise: Promise<ValueType>;
}
/**
Create a deferred promise.
*/
export default function pDefer<ValueType = unknown>(): DeferredPromise<
ValueType
>;

View File

@ -1,5 +1,6 @@
'use strict'; 'use strict';
module.exports = () => {
const pDefer = () => {
const ret = {}; const ret = {};
ret.promise = new Promise((resolve, reject) => { ret.promise = new Promise((resolve, reject) => {
@ -9,3 +10,6 @@ module.exports = () => {
return ret; return ret;
}; };
module.exports = pDefer;
module.exports.default = pDefer;

9
index.test-d.ts Normal file
View File

@ -0,0 +1,9 @@
import {expectType} from 'tsd-check';
import pDefer, {DeferredPromise} from '.';
expectType<DeferredPromise<unknown>>(pDefer());
expectType<DeferredPromise<string>>(pDefer<string>());
pDefer<string>().resolve('foo');
pDefer<string>().reject(new Error('foo'));
expectType<Promise<string>>(pDefer<string>().promise);

20
license
View File

@ -1,21 +1,9 @@
The MIT License (MIT) MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
Permission is hereby granted, free of charge, to any person obtaining a copy 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:
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 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 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.
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.

View File

@ -10,13 +10,14 @@
"url": "sindresorhus.com" "url": "sindresorhus.com"
}, },
"engines": { "engines": {
"node": ">=4" "node": ">=6"
}, },
"scripts": { "scripts": {
"test": "xo && ava" "test": "xo && ava && tsd-check"
}, },
"files": [ "files": [
"index.js" "index.js",
"index.d.ts"
], ],
"keywords": [ "keywords": [
"promise", "promise",
@ -32,10 +33,8 @@
"bluebird" "bluebird"
], ],
"devDependencies": { "devDependencies": {
"ava": "*", "ava": "^1.3.1",
"xo": "*" "tsd-check": "^0.5.0",
}, "xo": "^0.24.0"
"xo": {
"esnext": true
} }
} }

View File

@ -23,8 +23,10 @@ function delay(ms) {
return deferred.promise; return deferred.promise;
} }
delay(100).then(console.log); (async () => {
console.log(await delay(100));
//=> '🦄' //=> '🦄'
})();
``` ```
*The above is just an example. Use [`delay`](https://github.com/sindresorhus/delay) if you need to delay a promise.* *The above is just an example. Use [`delay`](https://github.com/sindresorhus/delay) if you need to delay a promise.*

View File

@ -1,14 +1,14 @@
import test from 'ava'; import test from 'ava';
import m from './'; import pDefer from '.';
const fixture = Symbol('fixture'); const fixture = Symbol('fixture');
function delay(ms) { function delay(ms) {
const deferred = m(); const deferred = pDefer();
setTimeout(deferred.resolve, ms, fixture); setTimeout(deferred.resolve, ms, fixture);
return deferred.promise; return deferred.promise;
} }
test(async t => { test('main', async t => {
t.is(await delay(50), fixture); t.is(await delay(50), fixture);
}); });