Skip to content

Instantly share code, notes, and snippets.

@qwtel
Last active March 26, 2017 19:45
Show Gist options
  • Select an option

  • Save qwtel/8bdd82eedec95f175c212031a20d7b53 to your computer and use it in GitHub Desktop.

Select an option

Save qwtel/8bdd82eedec95f175c212031a20d7b53 to your computer and use it in GitHub Desktop.
Make an observable take at least a certain time
const MIN_DURATION = 100;
ajaxRequest(href)
.zip(Observable.timer(MIN_DURATION), x => x))
.subscribe(response => {
// will execute after at least 100ms or longer depending on how long the request takes
});
@qwtel
Copy link
Author

qwtel commented Mar 26, 2017

Turn into an operator:

function ensureDuration(duration) {
  return this.zip(Observable.timer(duration), x => x)
}

Usage:

ajaxRquest(href)::ensureDuration(100)

Alternatively via monkey patching

Observable.prototype.ensureDuration = ensureDuration;
ajaxRquest(href).ensureDuration(100)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment