Skip to content

Instantly share code, notes, and snippets.

@Fenchurchh
Last active November 29, 2016 20:19
Show Gist options
  • Save Fenchurchh/8a7a21e8616716e13e3a997fcef89568 to your computer and use it in GitHub Desktop.
Save Fenchurchh/8a7a21e8616716e13e3a997fcef89568 to your computer and use it in GitHub Desktop.
const exec = require("promised-exec")
function onFailure(err) {
console.log("err", err)
}
function onSuccess(response) {
console.log("success", response)
}
/**
* timedPromise(duration)(promise) returns promise that rejects after duration ms
*/
let timedPromise = duration => fx => new Promise((fulfill, reject) => {
let timer = setTimeout(t => {
reject("Request took longer than the timeout of " + duration + " ms")
}, duration)
let promise = fx.then(response => {
fulfill(response)
clearTimeout(timer)
})
})
timedPromise(1000)(exec("ll"))
.then(onSuccess)
.catch(onFailure)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment