jsGiven/jsGiven

View on GitHub
js-given/src/async-actions.js

Summary

Maintainability
A
0 mins
Test Coverage
// @flow

let asyncActionsSingleton: Array<() => Promise<*>> = [];

export function executeStepAndCollectAsyncActions(
  stepActionThatMayCallDoAsync: () => any
): Array<() => Promise<*>> {
  asyncActionsSingleton = [];
  const collectedAsyncActions = [];

  try {
    stepActionThatMayCallDoAsync();
  } finally {
    collectedAsyncActions.push(...asyncActionsSingleton);
    asyncActionsSingleton = [];
  }

  return collectedAsyncActions;
}

export function doAsync(action: () => Promise<*>) {
  asyncActionsSingleton.push(action);
}