Reputation: 1394
Say we have an asynchronous function that may resolve within any duration. However fast it may be, we want to ensure it never resolves before t
time as passed. To achieve this in a reusable way, we create a function (here in JavaScript):
async function stall(task, minDuration) {
// Wait until both `task` and `delay(minDuration)` resolves
// and return the results from `task`
const results = await Promise.all([task, delay(minDuration)])
return results[0]
}
where delay()
is a simple function that resolves after a given amount of time.
Is there a canonical name for the stall()
function?
Upvotes: 1
Views: 64