├── .gitignore ├── .npmrc ├── .travis.yml ├── LICENSE ├── README.md ├── index.d.ts ├── index.js ├── package.json └── test └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .DS_Store* 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | node_js: 2 | - "14" 3 | - "12" 4 | language: node_js 5 | script: "npm run-script test-travis" 6 | after_script: "npm install coveralls@2 && cat ./coverage/lcov.info | coveralls" 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | 2 | The MIT License (MIT) 3 | 4 | Copyright (c) 2014 Jonathan Ong me@jongleberry.com 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # timeout-then 3 | 4 | [![NPM version][npm-image]][npm-url] 5 | [![Build status][travis-image]][travis-url] 6 | [![Test coverage][coveralls-image]][coveralls-url] 7 | [![Dependency Status][david-image]][david-url] 8 | [![License][license-image]][license-url] 9 | [![Downloads][downloads-image]][downloads-url] 10 | 11 | `setTimeout` as a promise. 12 | 13 | ```js 14 | const timeout = require('timeout-then'); 15 | 16 | timeout(100).then(function () { 17 | console.log('blah'); 18 | }); 19 | 20 | // clear timeout 21 | let timer = timeout(100); 22 | timer.then(function () { 23 | console.log('blah'); 24 | }); 25 | timer.clear(); 26 | ``` 27 | 28 | [gitter-image]: https://badges.gitter.im/thenables/timeout-then.png 29 | [gitter-url]: https://gitter.im/thenables/timeout-then 30 | [npm-image]: https://img.shields.io/npm/v/timeout-then.svg?style=flat-square 31 | [npm-url]: https://npmjs.org/package/timeout-then 32 | [github-tag]: http://img.shields.io/github/tag/thenables/timeout-then.svg?style=flat-square 33 | [github-url]: https://github.com/thenables/timeout-then/tags 34 | [travis-image]: https://img.shields.io/travis/thenables/timeout-then.svg?style=flat-square 35 | [travis-url]: https://travis-ci.org/thenables/timeout-then 36 | [coveralls-image]: https://img.shields.io/coveralls/thenables/timeout-then.svg?style=flat-square 37 | [coveralls-url]: https://coveralls.io/r/thenables/timeout-then 38 | [david-image]: http://img.shields.io/david/thenables/timeout-then.svg?style=flat-square 39 | [david-url]: https://david-dm.org/thenables/timeout-then 40 | [license-image]: http://img.shields.io/npm/l/timeout-then.svg?style=flat-square 41 | [license-url]: LICENSE 42 | [downloads-image]: http://img.shields.io/npm/dm/timeout-then.svg?style=flat-square 43 | [downloads-url]: https://npmjs.org/package/timeout-then 44 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | async function timeout(interval: number): Promise & { clear(): void } {} 2 | export default timeout 3 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = function (interval) { 2 | let timer 3 | let promise = new Promise(function (resolve) { 4 | timer = setTimeout(resolve, interval || 0) 5 | }) 6 | promise.clear = function () { 7 | return clearTimeout(timer) 8 | } 9 | return promise 10 | } 11 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "timeout-then", 3 | "description": "setTimeout as promised", 4 | "version": "2.0.0", 5 | "author": "Jonathan Ong (http://jongleberry.com)", 6 | "license": "MIT", 7 | "repository": "thenables/timeout-then", 8 | "devDependencies": { 9 | "istanbul": "^0.4.5", 10 | "mocha": "^8.0.1" 11 | }, 12 | "scripts": { 13 | "test": "mocha --reporter spec", 14 | "test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot", 15 | "test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter dot" 16 | }, 17 | "types": "index.d.ts", 18 | "keywords": [ 19 | "settimeout", 20 | "timeout", 21 | "promise", 22 | "then" 23 | ], 24 | "files": [ 25 | "index.d.ts", 26 | "index.js" 27 | ] 28 | } 29 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | 2 | const assert = require('assert') 3 | 4 | const timeout = require('..') 5 | 6 | it('should wait', function () { 7 | let blah = false 8 | 9 | setTimeout(function () { 10 | blah = true 11 | }, 5) 12 | 13 | return timeout(100).then(function () { 14 | assert(blah) 15 | }) 16 | }) 17 | 18 | it('should clear', function (done) { 19 | let blah = true 20 | 21 | let timer = timeout(5) 22 | timer.then(function () { 23 | blah = false 24 | }) 25 | timer.clear() 26 | 27 | setTimeout(function () { 28 | assert(blah) 29 | done() 30 | }, 100) 31 | }) 32 | --------------------------------------------------------------------------------