Skip to content

Instantly share code, notes, and snippets.

@nmn nmn/HOF1.ts
Created Apr 25, 2017

Embed
What would you like to do?
a higher-order-function that almost maintains the function signature, but not quite.
type ITask<I: $ReadOnlyArray<mixed>, O> = {
(...r: I): ?O,
lastError?: Error
}
function task <I: $ReadOnlyArray<mixed>, O>(inner: (...r: I) => O): ITask<I, O> {
const wrapped: any = function wrapped() {
try {
return (inner: any).apply(this, arguments)
} catch (err) {
(wrapped:any).lastError = err
}
}
wrapped.lastError = null
return wrapped
}
const t = task((str: string) => str.length)
t('hello') // good
t(123) // error, good
t(true) // error, good
console.log(t.lastError) // good
console.log(t.haha) // error, good
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.