Skip to content

preProcess and postProcess do not execute "in order" as documented for async functions #4961

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kfranqueiro opened this issue May 15, 2025 · 0 comments · May be fixed by #4962
Open

preProcess and postProcess do not execute "in order" as documented for async functions #4961

kfranqueiro opened this issue May 15, 2025 · 0 comments · May be fixed by #4962
Labels

Comments

@kfranqueiro
Copy link

kfranqueiro commented May 15, 2025

Description of problem

Given the following preProcess and postProcess examples within a respec config:

var respecConfig = {
  // ...
  preProcess: [
    async () => {
      await new Promise((resolve) => setTimeout(resolve, 1000));
      console.log("pre async");
    },
    () =>
      new Promise((resolve) => setTimeout(resolve, 1000)).then(() =>
        console.log("pre promise")
      ),
    () => {
      console.log("pre sync");
    },
  ],
  postProcess: [
    async () => {
      await new Promise((resolve) => setTimeout(resolve, 1000));
      console.log("post async");
    },
    () =>
      new Promise((resolve) => setTimeout(resolve, 1000)).then(() =>
        console.log("post promise")
      ),
    () => {
      console.log("post sync");
    },
  ]

The above config defines 3 functions for each of preProcess and postProcess:

  • One defined as an async function, which resolves after a 1-second delay
  • One defined as a function that returns a promise, which resolves after a 1-second delay (the same behavior as the first function, just defined differently)
  • One defined as a function that returns immediately

Expected Behavior

Based on the documentation (which states functions run in order), I expect this:

pre async
pre promise
pre sync
post async
post promise
post sync

Actual Behavior

What actually happens is this:

14:18:39.971 pre sync
14:18:40.979 pre async
14:18:40.980 pre promise
14:18:41.119 post sync
14:18:42.119 post async
14:18:42.120 post promise

(Timestamps are included above to demonstrate that the async functions finish around a second after the sync function starts, indicating they start around the same time, given the async functions' 1-second wait time.)

Root Cause

This occurs because the implementations for both properties use Promise.all on a mapped array which has already immediately invoked each function up-front. To preserve order, these functions' invocations would need to be sequentially chained instead.

Aside: It is worth noting that the documentation itself does not seem to explicitly indicate that preProcess and postProcess support promises; it seems it was never updated when that functionality was added.


I would be interested in working on a PR to fix this if one would be accepted. (Edit: I initially had problems getting tests to run, but forcing karma to run with --browsers Firefox helped in my case, since I don't have Chrome installed on this machine.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant