Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

promto

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

promto - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

CHANGELOG.md

36

package.json
{
"name": "promto",
"version": "1.0.1",
"description": "Create a promise with timeout",
"main": "src/index.js",
"version": "1.1.0",
"description": "Create a promise with a timeout.",
"build": "build/index.js",
"module": "src/index.js",
"scripts": {
"test": "zoroaster test/spec"
"t": "zoroaster -a",
"test": "yarn t test/spec",
"spec": "yarn t test/spec",
"mask": "yarn t test/mask",
"test-build": "ALAMODE_ENV=test-build yarn test",
"lint": "eslint .",
"doc": "NODE_DEBUG=doc doc -o README.md",
"b": "alamode src -o build -s",
"d": "yarn-s d1",
"d1": "typal src/index.js -c",
"externs": "typal externs.js -e",
"build": "yarn-s d b doc",
"e": "alanode",
"example/": "yarn e example/example.js"
},
"files": [
"src"
],
"repository": {
"type": "git",
"url": "git+https://github.com/Sobesednik/promto.git"
"url": "git://github.com/artdecocode/promto.git"
},
"keywords": [
"promise",
"timeout"
"timeout",
"async"
],
"author": "Anton <anton@sobesednik.media>",
"author": "Anton <anton@adc.sh>",
"license": "MIT",

@@ -24,4 +42,6 @@ "bugs": {

"devDependencies": {
"zoroaster": "^0.4.3"
"alamode": "^2.0.0",
"documentary": "^1.26.2",
"zoroaster": "^3.13.0"
}
}
# promto
[![npm version](https://badge.fury.io/js/promto.svg)](https://badge.fury.io/js/promto)
[![npm version](https://badge.fury.io/js/promto.svg)](https://npmjs.org/package/promto)
Promise with timeout.
`promto` Wrappes A Promise In Timeout.
## _promto(promise:Promise, timeout:number, description: string): Promise_
```sh
yarn add promto
```
Create a promise which should timeout after N milliseconds. It will be resolved with promise value
or rejected with promise error when not timed out, and rejected with timeout error otherwise.
## Table Of Contents
### Timeout rejection
- [Table Of Contents](#table-of-contents)
- [API](#api)
- [`async promto(promise: Promise, timeout: number, description: string?): Promise`](#async-promtopromise-promisetimeout-numberdescription-string-promise)
- [Copyright](#copyright)
```js
const promto = require('promto')
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/0.svg?sanitize=true"></a></p>
const promise = new Promise(resolve => setTimeout(resolve, 200))
const promiseWithTimeout = promto(promise, 100, 'Example Promise')
## API
promiseWithTimeout.then(console.log, console.error)
// Error: Example Promise has timed out after 100ms
```
The package is available by importing its default function:
### Resolve value
```js
const promto = require('promto')
import promto from 'promto'
```
const promise = new Promise(resolve => setTimeout(
() => resolve('Promise Result'),
50
))
const promiseWithTimeout = promto(promise, 100, 'Example Promise')
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/1.svg?sanitize=true"></a></p>
promiseWithTimeout.then(console.log, console.error)
// Promise Result
```
## `async promto(`<br/>&nbsp;&nbsp;`promise: Promise,`<br/>&nbsp;&nbsp;`timeout: number,`<br/>&nbsp;&nbsp;`description: string?,`<br/>`): Promise`
### Rejection error
Creates a new promise which will be rejected upon timeout (after N milliseconds). It will be resolved with the promise value or rejected with the promise error when not timed out.
```js
const promto = require('promto')
import promto from 'promto'
const promise = Promise.reject(new Error('Promise Error'))
const promiseWithTimeout = promto(promise, 100, 'Example Promise')
const makePromise = async (timeout, result) => {
await new Promise((resolve) => {
setTimeout(resolve, timeout || 200)
})
if (result instanceof Error) throw result
return result
}
promiseWithTimeout.then(console.log, console.error)
// Error: Promise Error
(async () => {
// ok
const res = await promto(makePromise(50, 'hello'), 100)
console.log(res)
// rejected timeout
try {
await promto(makePromise(150, 'world'), 100)
} catch (err) {
console.log(err.message)
}
// rejected timeout with description
try {
await promto(makePromise(150, 'world'), 100, 'Example')
} catch (err) {
console.log(err.message)
}
// rejected promise
try {
await promto(makePromise(50, new Error('Error in promise')), 100)
} catch (err) {
console.log(err.message)
}
})()
```
```
hello
Promise has timed out after 100ms
Example has timed out after 100ms
Error in promise
```
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/2.svg?sanitize=true"></a></p>
## Copyright
2017, [Sobesednik Media](https://sobesednik.media)
<table>
<tr>
<th>
<a href="https://artd.eco">
<img src="https://raw.githubusercontent.com/wrote/wrote/master/images/artdeco.png" alt="Art Deco" />
</a>
</th>
<th>© <a href="https://artd.eco">Art Deco</a> 2019</th>
<th>
<a href="https://www.technation.sucks" title="Tech Nation Visa">
<img src="https://raw.githubusercontent.com/artdecoweb/www.technation.sucks/master/anim.gif"
alt="Tech Nation Visa" />
</a>
</th>
<th><a href="https://www.technation.sucks">Tech Nation Visa Sucks</a></th>
</tr>
</table>
<p align="center"><a href="#table-of-contents"><img src=".documentary/section-breaks/-1.svg?sanitize=true"></a></p>

@@ -1,72 +0,42 @@

'use strict'
function isString(value) {
return (typeof value).toLowerCase() === 'string'
}
function isNumber(value) {
return (typeof value).toLowerCase() === 'number'
}
function createTimeout(desc, timeout, cb) {
return setTimeout(() => {
const message = `${isString(desc) ? desc : 'Promise'} has timed out after ${timeout}ms`
const err = new Error(message)
err.stack = `Error: ${message}`
cb(err)
}, timeout)
return setTimeout(() => {
const message = `${desc ? desc : 'Promise'} has timed out after ${timeout}ms`
const err = new Error(message)
err.stack = err.message
cb(err)
}, timeout)
}
function makeTimeoutPromise(desc, timeout) {
let to
const promise = new Promise((_, reject) => {
to = createTimeout(desc, timeout, reject)
})
return { timeout: to, promise }
let to
const promise = new Promise((_, reject) => {
to = createTimeout(desc, timeout, reject)
})
return { timeout: to, promise }
}
/**
* Clear timeout and return / throw. Useful in .then()
* @param {object} timeout Timeout handle returned by setTimeout
* @param {any} [res] What to return after clearing
* @param {Error} [err] What to throw after clearing
*/
function transientClearTimeout(timeout, res, err) {
clearTimeout(timeout)
if (err) throw err
return res
}
/**
* Create a promise which will be rejected after a timeout.
* @param {Promise} promise A promise to race with
* @param {Number} timeout Timeout in ms after which to reject
* @param {string} desc Description of a promise to be printed in error
* @returns {Promise} A promise with a timeout
* @param {!Promise} promise A promise to race with
* @param {number} timeout Timeout in ms after which to reject
* @param {string} [desc] Description of a promise to be printed in error
* @returns {!Promise} A promise with a timeout
*/
function createPromiseWithTimeout(promise, timeout, desc) {
try {
if (!(promise instanceof Promise)) {
throw new Error('Promise expected')
}
if (!isNumber(timeout)) {
throw new Error('Timeout must be a number')
}
if (timeout < 0) {
throw new Error('Timeout cannot be negative')
}
} catch (err) {
return Promise.reject(err)
}
export default async function createPromiseWithTimeout(promise, timeout, desc) {
if (!(promise instanceof Promise))
throw new Error('Promise expected')
if (!timeout)
throw new Error('Timeout must be a number')
if (timeout < 0)
throw new Error('Timeout cannot be negative')
const timeoutPromise = makeTimeoutPromise(desc, timeout)
return Promise.race([
promise,
timeoutPromise.promise,
]).then(
transientClearTimeout.bind(null, timeoutPromise.timeout),
transientClearTimeout.bind(null, timeoutPromise.timeout, null)
)
}
module.exports = createPromiseWithTimeout
const { promise: toPromise, timeout: to } = makeTimeoutPromise(desc, timeout)
try {
return await Promise.race([
promise,
toPromise,
])
} finally {
clearTimeout(to)
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc