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.

var timeout = require('when/timeout');

function doSomething() {

	// Kickoff a slow, asynchronous calculation
	var slowCalculationPromise = 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 doSlowAsyncCalculation
	return timeout(5000, slowCalculationPromise);
}
Clone this wiki locally