promto
Create a promise with timeout
Last updated 8 years ago by zvr .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install promto 
SYNC missed versions from official npm registry.

promto

npm version

promto Wrappes A Promise In Timeout.

yarn add promto

Table Of Contents

API

The package is available by importing its default function:

import promto from 'promto'

async promto(
  promise: Promise,
  timeout: number,
  description: string?,
): Promise

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.

import promto from 'promto'

/**
 * @param {number} timeout
 * @param {string|Error} result
 */
const makePromise = async (timeout, result) => {
  await new Promise((resolve) => {
    setTimeout(resolve, timeout || 200)
  })
  if (result instanceof Error) throw result
  return result
}

(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

Copyright

Art Deco © Art Deco 2019 Tech Nation Visa Tech Nation Visa Sucks

Current Tags

  • 1.2.0                                ...           latest (5 years ago)

6 Versions

  • 1.2.0                                ...           5 years ago
  • 1.1.2                                ...           6 years ago
  • 1.1.1                                ...           6 years ago
  • 1.1.0                                ...           6 years ago
  • 1.0.1                                ...           8 years ago
  • 1.0.0                                ...           8 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 6
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (1)
Dependents (1)

Copyright 2013 - present © cnpmjs.org | Home |