Code Monkey home page Code Monkey logo

await-timeout's People

Contributors

dependabot[bot] avatar vitalets avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

await-timeout's Issues

Supporting AbortController

It would also be nice to consider supporting AbortController which would allow the API to be web compatible (cancelling a timer and a fetch at the same time for instance):

const controller = new AbortController()
const timer = Timeout.set(1000, { signal: controller.signal });
signal.abort();

From #3 (comment)

Question about clear timeout

Hello, I have a small quesiton about this code:

import Timeout from 'await-timeout';

const timer = new Timeout();
try {
  await Promise.race([
    fetch('https://example.com'),
    timer.set(1000, 'Timeout!')
  ]);
} finally {
  timer.clear();
}

Without a timer cleanup you may get unexpected effects in you code - as all promises in Promise.race are get fulfille

Why the timer need to be cleanup? race() only accept the first settled promise.

Calling timer.set() several times

Hello!

I've found some unobvious behavior of await-timeout during its inclusion in our project. And want to discuss them with you and community.

So the first my example is:

var awaitTimeout = require("await-timeout");
var timer = new awaitTimeout();

timer.set(5000).then(() => console.log('TIMER'));
setTimeout(() => {
    timer.set(13000);
}, 2000);

This block of code will log TIMER after 5 sec. But at first I thought that it logs TIMER after 15 (2 + 13) sec. because second timer.set(13000) call postpones timer. But in result this second call doesn't influence.

Next example:

var awaitTimeout = require("await-timeout");

var timer = new awaitTimeout();

timer.set(5000).then(() => console.log('TIMER1'));
setTimeout(() => {
    timer.set(5000).then(() => console.log('TIMER2'));
    timer.clear();
}, 2000);

It will log TIMER1, but TIMER2 won't never be logged.

If I understood it correct setting timer is available only once until timer clearing.
Maybe timer has to throw an error in that case then.
And I think it would be great to clarify this cases in Readme.
What do you think?

API

Wouldn't it be better to have:

const timer = Timeout.set(1000); // set the timer
Timeout.clear(timer); // clear the timer

?

How to cancel original function (async/await)

It runs the test() function start to end, even though it times out. How do I make it abort the function that timed out? Similar to what Bluebird does with the {cancellation: true} feature, but using async/await.

// const Promise = require('bluebird');
// Promise.config({cancellation: true});

const Timeout = require('await-timeout');
const delay = ms => new Promise(_ => setTimeout(_, ms))

 
async function test() {
  console.log('start')
  await delay(2000)
  console.log('end')
}

async function run() {
  const timer = new Timeout();
  try {
    await Promise.race([
      test(),
      timer.set(1200, 'Timeout!')
    ]);
  } catch (e) {
    console.log(e);
  } finally {
    timer.clear();
  }
}

run();

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.