Skip to content

Instantly share code, notes, and snippets.

@JakeGinnivan
Last active September 4, 2019 09:24
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 JakeGinnivan/1f1c5e05ab9d1cce12da97ec42731381 to your computer and use it in GitHub Desktop.
Save JakeGinnivan/1f1c5e05ab9d1cce12da97ec42731381 to your computer and use it in GitHub Desktop.
export class PromiseTracker {
promises = []
track(promise) {
this.promises.push(promise)
}
middleware() {
return () => next => (action) => {
const result = next(action)
// Promise.resolve must return the same promise if the arg is a promise
// http://www.ecma-international.org/ecma-262/6.0/#sec-promise.resolve
if (Promise.resolve(result) === result) {
this.track(result)
}
return result
}
}
hasWork() {
return this.promises.length > 0
}
waitForCompletion() {
const all = Promise.all([...this.promises])
// Use setTimeout to put the resolution of this
// promise back onto the event loop, this can fix
// issues where tests have not re-rendered before
// trying to find elements
.then(() => new Promise(setTimeout))
this.promises = []
return all
}
}
// Usage
const promiseTracker = new PromiseTracker()
const store = configureStore(
rootReducer,
{},
promiseTracker.middleware(),
thunk
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment