# Interval Promise
[![NPM Version](https://img.shields.io/npm/v/interval-promise.svg)](https://npmjs.org/package/interval-promise)
[![Build Status](https://travis-ci.org/andyfleming/interval-promise.svg?branch=master)](https://travis-ci.org/andyfleming/interval-promise)
[![Coverage Status](https://coveralls.io/repos/github/andyfleming/interval-promise/badge.svg?branch=master)](https://coveralls.io/github/andyfleming/interval-promise?branch=master)
## Overview
This library provides a simple mechanism for running a promise with a given amount of time between executions.
#### Standard Javascript » setInterval()
![traditional interval](https://user-images.githubusercontent.com/721038/33246371-9d0a6e56-d2c8-11e7-9787-cd67354c9f38.png)
#### interval-promise » interval()
![interval promise](https://user-images.githubusercontent.com/721038/33246370-9cf05a20-d2c8-11e7-816c-744ee6b5a094.png)
## Installation
```bash
npm install interval-promise
```
## Usage
**Simple example using async-await**
```javascript
const interval = require('interval-promise')
// Run a function 10 times with 1 second between each iteration
interval(async () => {
await someOtherPromiseReturningFunction()
await another()
}, 1000, {iterations: 10})
```
## API
```javascript
interval(func, intervalLength, options = {}) // returns Promise<undefined>
```
### Arguments
<table>
<thead>
<tr>
<th>Argument</th>
<th>Attritubes</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code><b>func</b></code></td>
<td><i>function</i></td>
<td><b>Required</b><br>Function to execute for each interval. <i>MUST</i> return a promise.<br><br>Two arguments are passed to this function.
<ul>
<li><code>iterationNumber</code> <i>number</i> — The iteration number (starting at 1)</li>
<li><code>stop</code> <i>function</i> — used to "stop" (skipping all remaining iterations)</li>
</ul>
</td>
</tr>
<tr>
<td><code><b>intervalLength</b></code></td>
<td><i>number | function</i></td>
<td><b>Required</b><br>Length in ms to wait between iterations. Should be (or return) a non-negative integer.<br><br>If a function is used, one parameter <code>iterationNumber</code> (starting at 1) is passed.</td>
</tr>
<tr>
<td><code><b>options</b></code></td>
<td><i>object</i></td>
<td>Optional settings (detailed below).
<tr>
<td><code>options.<b>iterations</b></code></td>
<td><i>number</i></td>
<td><b>Default: </b><code>Infinity</code><br>The number of times to execute the function. Must be Infinity or an integer greater than 0.</td>
</tr>
<tr>
<td><code>options.<b>stopOnError</b></code></td>
<td><i>boolean</i></td>
<td><b>Default: </b><code>true</code><br>If true, no subsequent calls will be made. The promise returned by interval() will be rejected and pass through the error thrown.</td>
</tr>
</tbody>
</table>
## Project Values
* **Approachability** — Basic usage should be concise and readable.
* **Debuggability** — Error feedback should be helpful and error handling options should be flexible.
* **Stability** — Functionality should be well-tested and reliable.
## Acknowledgements
This library was inspired by [reissue](https://github.com/DonutEspresso/reissue).
## FAQ
### How can I stop the interval from _outside_ the interval function?
There isn't currently direct feature to stop the iterations externally. You can, however, achieve this by checking a variable in the parent scope (of where the function is defined). Check out the code below.
```js
const interval = require('interval-promise')
let stoppedExternally = false
const stopExternally = () => { stoppedExternally = true }
interval(async (iteration, stop) => {
if (stoppedExternally) {
stop()
}
// ... normal functionality ...
}, 1000)
// Some other work...
someOtherWork().then(() => {
// Now that our "other work" is done, we can stop our interval above with:
stopExternally()
})
```
没有合适的资源?快使用搜索试试~ 我知道了~
interval-promise:带有setTimeout语义的setInterval用于promise和asyncawait
共15个文件
js:8个
json:2个
yml:1个
需积分: 50 1 下载量 164 浏览量
2021-04-30
06:56:17
上传
评论
收藏 67KB ZIP 举报
温馨提示
间隔承诺 概述 该库提供了一种简单的机制,可以在两次执行之间以给定的时间运行诺言。 标准Javascript»setInterval() 间隔承诺»interval() 安装 npm install interval-promise 用法 使用async-await的简单示例 const interval = require ( 'interval-promise' ) // Run a function 10 times with 1 second between each iteration interval ( async ( ) => { await someOtherPromiseReturningFunction ( ) await another ( ) } , 1000 , { iterations : 10 } ) 原料药 interval ( func
资源详情
资源评论
资源推荐
收起资源包目录
interval-promise-master.zip (15个子文件)
interval-promise-master
.gitignore 904B
package.json 884B
package-lock.json 219KB
src
index.js 5KB
index.d.ts 480B
.travis.yml 257B
LICENSE 1KB
.eslintrc.js 429B
README.md 4KB
tests
execution.test.js 772B
user-function-arguments.test.js 827B
stop-on-error.test.js 736B
arg-validation.test.js 4KB
runtime-validation.test.js 966B
calculated-interval.test.js 820B
共 15 条
- 1
jackie陈
- 粉丝: 12
- 资源: 4597
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- record record record record record record record record record
- Android 启动提示Android 正在升级...提示源码分析
- pojie-drawio-confluence-plugin-9.5.8.obr
- 信息学奥赛2020年NOIP真题
- SunloginClient-15.1.0.58718-x64.exe
- 信息学奥赛2021年NOIP真题
- 星辰语义大模型TeleChat超详细部署文档手册
- python range函数.pdf
- 许子昊毕业论文初稿-基于响应式编程的水质设备监测平台的设计与实现-定稿格式.docx
- musicManager.jsp
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0