Skip to content
Trevor Senior edited this page Dec 3, 2013 · 3 revisions

The when/timeout helper creates a promise that automatically rejects after a specified time, if not resolved before that. This allows you to setup up scenarios where a computation must finish within a certain time, or be rejected.

vartimeout=require('when/timeout');functiondoSomething(){// Kickoff a slow, asynchronous calculationvarslowCalculationPromise=when(doSlowAsyncCalculation());// Return a new promise that will resolve if doSlowAsyncCalculation finishes// in under 5 seconds, but will reject if it doesn't.// The resolution value will be the result of doSlowAsyncCalculationreturntimeout(5000,slowCalculationPromise);}

Clone this wiki locally