Skip to content

Instantly share code, notes, and snippets.

@gilesbradshaw
Created January 21, 2019 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gilesbradshaw/2e6e9fc40b3ca1fa35f4d5f9d460aec3 to your computer and use it in GitHub Desktop.
Save gilesbradshaw/2e6e9fc40b3ca1fa35f4d5f9d460aec3 to your computer and use it in GitHub Desktop.
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