Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
use promises to serialise access to an action
// serialises access to an action
export default () => {
// promise to allow the next action
let next = Promise.resolve()
return (action) => {
let nextResolve
const nextNext = () => new Promise(
(resolve, reject) =>
action()
.then(
(x) => {
resolve(x)
// allow next action
nextResolve()
},
)
.catch(reject),
)
const myNext = next
next = new Promise(
(resolve) => {
nextResolve = resolve
},
)
return myNext
.then(nextNext)
}
}
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.