Skip to content

[Bug?]: V2 middleware functions do not execute in order #2131

@sabercoy

Description

@sabercoy

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

onRequest functions execute in order
onBeforeResponse functions execute in inconsistent (backwards?) order

Expected behavior 🤔

onBeforeResponse functions execute in order

Steps to reproduce 🕹

Steps:

  1. create new V2 project
  2. create middleware file with this code
import { createMiddleware } from '@solidjs/start/middleware'

const one = async () => {
  console.log('begin one')
  await new Promise(resolve => setTimeout(resolve, Math.random() * 1000))
  console.log('end one')
}

const two = async () => {
  console.log('begin two')
  await new Promise(resolve => setTimeout(resolve, Math.random() * 1000))
  console.log('end two')
}

const three = async () => {
  console.log('begin three')
  await new Promise(resolve => setTimeout(resolve, Math.random() * 1000))
  console.log('end three')
}

const four = async () => {
  console.log('begin four')
  await new Promise(resolve => setTimeout(resolve, Math.random() * 1000))
  console.log('end four')
}

const five = async () => {
  console.log('begin five')
  await new Promise(resolve => setTimeout(resolve, Math.random() * 1000))
  console.log('end five')
}

const six = async () => {
  console.log('begin six')
  await new Promise(resolve => setTimeout(resolve, Math.random() * 1000))
  console.log('end six')
}

const seven = async () => {
  console.log('begin seven')
  await new Promise(resolve => setTimeout(resolve, Math.random() * 1000))
  console.log('end seven')
}

const eight = async () => {
  console.log('begin eight')
  await new Promise(resolve => setTimeout(resolve, Math.random() * 1000))
  console.log('end eight')
}

const nine = async () => {
  console.log('begin nine')
  await new Promise(resolve => setTimeout(resolve, Math.random() * 1000))
  console.log('end nine')
}

const ten = async () => {
  console.log('begin ten')
  await new Promise(resolve => setTimeout(resolve, Math.random() * 1000))
  console.log('end ten')
}

export default createMiddleware({
  onRequest: [
    one,
    two,
    three,
    four,
  ],
  onBeforeResponse: [
    five,
    six,
    seven,
    eight,
    nine,
    ten,
  ]
})
  1. observe how it is different than V1

Context 🔦

trying to migrate to V2 and realized some middleware functions were breaking because values they expected were supposed to be populated by previous functions, but those functions were not called yet because it is happening out of order

Your environment 🌎

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions