sequence.js 950 B

123456789101112131415161718192021222324
  1. // Like Bash, we await both subprocesses. This is unlike some other shells which only await the destination subprocess.
  2. // Like Bash with the `pipefail` option, if either subprocess fails, the whole pipe fails.
  3. // Like Bash, if both subprocesses fail, we return the failure of the destination.
  4. // This ensures both subprocesses' errors are present, using `error.pipedFrom`.
  5. export const waitForBothSubprocesses = async subprocessPromises => {
  6. const [
  7. {status: sourceStatus, reason: sourceReason, value: sourceResult = sourceReason},
  8. {status: destinationStatus, reason: destinationReason, value: destinationResult = destinationReason},
  9. ] = await subprocessPromises;
  10. if (!destinationResult.pipedFrom.includes(sourceResult)) {
  11. destinationResult.pipedFrom.push(sourceResult);
  12. }
  13. if (destinationStatus === 'rejected') {
  14. throw destinationResult;
  15. }
  16. if (sourceStatus === 'rejected') {
  17. throw sourceResult;
  18. }
  19. return destinationResult;
  20. };