Repository: vercel/next.js
File: turbopack/crates/turbopack-core/src/resolve/mod.rs (Rust)

Code before:
            continue;
        }

        if let Some(result) = *plugin
            .before_resolve(lookup_path.clone(), reference_type.clone(), request)
            .await?
        {
            return Ok(Some(*result));
        }
    }
    Ok(None)
}

#[tracing::instrument(level = "trace", skip_all)]
async fn handle_after_resolve_plugins(
    lookup_path: FileSystemPath,
    reference_type: ReferenceType,
    request: Vc<Request>,
    options: Vc<ResolveOptions>,
    result: Vc<ResolveResult>,
) -> Result<Vc<ResolveResult>> {
    // Pre-fetch options to avoid repeated await calls in the inner loop
    let options_value = options.await?;

    // Pre-resolve all plugin conditions once to avoid repeated resolve calls in the loop
    let mut resolved_conditions: Vec<AfterResolvePluginWithCondition> =
        Vec::with_capacity(options_value.after_resolve_plugins.len());
    for plugin in &options_value.after_resolve_plugins {
        let condition = plugin.after_resolve_condition().resolve().await?;
        resolved_conditions.push((*plugin, condition));
    }

    async fn apply_plugins_to_path(
        path: FileSystemPath,
        lookup_path: FileSystemPath,
        reference_type: ReferenceType,
        request: Vc<Request>,
        plugins_with_conditions: &[AfterResolvePluginWithCondition],
    ) -> Result<Option<Vc<ResolveResult>>> {
        for (plugin, after_resolve_condition) in plugins_with_conditions {
            if *after_resolve_condition.matches(path.clone()).await?
                && let Some(result) = *plugin
                    .after_resolve(
                        path.clone(),
                        lookup_path.clone(),
                        reference_type.clone(),
                        request,
                    )
                    .await?
            {
                return Ok(Some(*result));

Review comment (suggestion):
Any reason why you didn't use try_join here? `after_resolve_plugins.iter().map(async |p| Ok((*plugin, after_resolve_condition().resolve().await?)).try_join().await?`

Code after:
            continue;
        }

        if let Some(result) = *plugin
            .before_resolve(lookup_path.clone(), reference_type.clone(), request)
            .await?
        {
            return Ok(Some(*result));
        }
    }
    Ok(None)
}

#[tracing::instrument(level = "trace", skip_all)]
async fn handle_after_resolve_plugins(
    lookup_path: FileSystemPath,
    reference_type: ReferenceType,
    request: Vc<Request>,
    options: Vc<ResolveOptions>,
    result: Vc<ResolveResult>,
) -> Result<Vc<ResolveResult>> {
    // Pre-fetch options to avoid repeated await calls in the inner loop
    let options_value = options.await?;

    // Pre-resolve all plugin conditions once to avoid repeated resolve calls in the loop
    let resolved_conditions = options_value
        .after_resolve_plugins
        .iter()
        .map(async |p| Ok((*p, p.after_resolve_condition().resolve().await?)))
        .try_join()
        .await?;

    async fn apply_plugins_to_path(
        path: FileSystemPath,
        lookup_path: FileSystemPath,
        reference_type: ReferenceType,
        request: Vc<Request>,
        plugins_with_conditions: &[AfterResolvePluginWithCondition],
    ) -> Result<Option<Vc<ResolveResult>>> {
        for (plugin, after_resolve_condition) in plugins_with_conditions {
            if *after_resolve_condition.matches(path.clone()).await?
                && let Some(result) = *plugin
                    .after_resolve(
                        path.clone(),
                        lookup_path.clone(),
                        reference_type.clone(),
                        request,
                    )
                    .await?
            {
                return Ok(Some(*result));