File size: 893 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import { InvariantError } from '../../shared/lib/invariant-error'
/**
* This is a utility function to make scheduling sequential tasks that run back to back easier.
* We schedule on the same queue (setImmediate) at the same time to ensure no other events can sneak in between.
*/
export function scheduleInSequentialTasks<R>(
render: () => R | Promise<R>,
followup: () => void
): Promise<R> {
if (process.env.NEXT_RUNTIME === 'edge') {
throw new InvariantError(
'`scheduleInSequentialTasks` should not be called in edge runtime.'
)
} else {
return new Promise((resolve, reject) => {
let pendingResult: R | Promise<R>
setImmediate(() => {
try {
pendingResult = render()
} catch (err) {
reject(err)
}
})
setImmediate(() => {
followup()
resolve(pendingResult)
})
})
}
}
|