Executes a function. If the function calls discard, then it is retried, up to a certain number of times.