canjs / can-zone

A context for tracking asynchronous activity in JavaScript applications.
https://v4.canjs.com/doc/can-zone.html
MIT License
92 stars 4 forks source link

can-zone/timeout #71

Closed matthewp closed 8 years ago

matthewp commented 8 years ago

This is a plugin that will provide timeout capabilities.

matthewp commented 8 years ago

Having trouble deciding on the API. Here's a rough cut of how it would be used for server-side rendering with an explanation:

var zone = new Zone({
  plugins: [require("can-zone/timeout")]
});

var timeoutPromise = zone.timeout(5000);
var runPromise = zone.run(function(){
  render()
});

Promise.race([
  timeoutPromise,
  runPromise
]).then(function(){
  res.send(doc.innerHTML);
});

runPromise.then(function(){
  cleanup();
});

The reason we need both promises is because we need to do cleanup after the runPromise has completed, even if it takes longer than the timeout.