hold-up icon indicating copy to clipboard operation
hold-up copied to clipboard

A way to support waiting/success/failed ternary

Open keul opened this issue 7 years ago • 2 comments

What I find missing in this lib is a failCondition, let say a new signature like:

holdUp(condition, [timeoutInterval], [retryInterval], [failCondition])

My usecase: I'm waiting from a global function to be loaded from another module so I call:

await holdUp(
   () => whatever,
   30000,
);

However is possible that this additional module will not be loaded at all. So I don't need to wait for 30 secs timeout to know that the function will not be there as I have a way to know it from the beginning (or few millisecs after).

In this case the current implementation will still wait for 30 secs, and I'll get a general Timeout.

What I'm thinking: a new failCondition function, still checked every retryInterval, but when truthy will immediately terminate waiting.

keul avatar Sep 07 '18 10:09 keul

Hum, this is an interesting use case...

Maybe providing something like a signal to cancel it would do it?

function check(task) {
  if (something) task.fail()
}

await holdUp(check, 3000)

Then you could also do things like:

const task = await holdUp(check, 3000)

if (something) task.fail()

What do you think?

rafaelrinaldi avatar Sep 07 '18 22:09 rafaelrinaldi

That's would be an elegant solution! 👍

keul avatar Sep 08 '18 08:09 keul