promise-timeout

2018-11-10 admin

promise-timeout是什么

什么是promise-timeout,Simple timeouts for promises

promise-timeout使用教程帮助文档

promise-timeout

A super-simple way to put a timeout on promise resolution.

It assumes you already have either platform support for promises (Node 0.12 or greater), or you have a polyfill (see es6-promise).

Installation

$ npm install promise-timeout

Usage

timeout(promise, timeoutMillis)

Rejects a promise with a TimeoutError if it does not settle within the specified timeout. Parameters:

  • promise: Promise - Promise to monitor.
  • timeoutMillis: number - Number of milliseconds to wait on settling.

TimeoutError

Exception indicating that the timeout expired.

Examples

ES2015:

import { timeout, TimeoutError } from 'promise-timeout';

let somePromise = goDoSomething();

timeout(somePromise, 1000)
  .then((thing) => console.log('I did a thing!'))
  .catch((err) => {
    if (err instanceof TimeoutError) {
      console.error('Timeout :-(');
    }
  });

ES5:

'use strict';

var pt = require('promise-timeout');

var somePromise = goDoSomething();

pt.timeout(somePromise, 1000)
  .then(function (thing) {
    console.log('I did a thing!');
  }).catch(function (err) {
    if (err instanceof pt.TimeoutError) {
      console.error('Timeout :-(');
    }
  });

本站文章除注明转载外,均为本站原创或编译。欢迎任何形式的转载,但请务必注明出处。

转载请注明:文章转载自 JavaScript中文网 [https://www.javascriptcn.com]

本文地址:https://www.javascriptcn.com/read-45078.html

文章标题:promise-timeout

回到顶部