<commit_before>import intersection from 'lodash/intersection';
import Genotypes from '@/snappy/Genotypes';
import Genosets from '@/snappy/Genosets';
import File from '@/snappy/File';

async function process(fsnps) {
  const file = new File('', fsnps);
  const genosets = await Genosets.getSupportedGenosets();
  const snps = await Genotypes.getSupportedSnps();
  const promises = [];

  const commonSnps = intersection(
    Object.keys(file.snps),
    snps,
  );
  for (const snp of commonSnps) {
    const genotype = file.normalizedSnps[snp].genotype;
    if (genotype !== '??') {
      promises.push(Genotypes.get(snp, genotype));
    }
  }

  for (const genoset of genosets) {
    promises.push(new Promise((r, rj) => {
      Genosets.get(genoset).then((gs) => {
        if (gs.match(file)) {
          return r(gs);
        }
        return rj();
      });
    }));
  }

  const rPromises = promises.map(p => p
    .then(data => ({
      status: 'resolved',
      data,
    }))
    .catch(error => ({
      status: 'rejected',
      error,
    })),
  );

  const reflected = await Promise.all(rPromises);
  const results = [];
  for (const r of reflected) {
    if (r.status === 'resolved') {
      results.push(Object.assign({}, r.data));
    }
  }

  return postMessage({
    state: 'done',
    results: JSON.stringify(results),
  });<commit_msg>workers/Analyzer.js: Fix race condition<commit_after>import intersection from 'lodash/intersection';
import Genotypes from '@/snappy/Genotypes';
import Genosets from '@/snappy/Genosets';
import File from '@/snappy/File';

async function process(fsnps) {
  const file = new File('', fsnps);
  const genosets = await Genosets.getSupportedGenosets();
  const snps = await Genotypes.getSupportedSnps();
  const promises = [];

  const reflect = p => p
    .then(data => ({
      status: 'resolved',
      data,
    }))
    .catch(error => ({
      status: 'rejected',
      error,
    }));

  const commonSnps = intersection(
    Object.keys(file.snps),
    snps,
  );
  for (const snp of commonSnps) {
    const genotype = file.normalizedSnps[snp].genotype;
    if (genotype !== '??') {
      promises.push(reflect(Genotypes.get(snp, genotype)));
    }
  }

  for (const genoset of genosets) {
    promises.push(new Promise((r, rj) => {
      Genosets.get(genoset).then((gs) => {
        if (gs.match(file)) {
          return r(gs);
        }
        return rj();
      });
    }));
  }

  const reflected = await Promise.all(promises);
  const results = [];
  for (const r of reflected) {
    if (r.status === 'resolved') {
      results.push(Object.assign({}, r.data));
    }
  }

  return postMessage({
    state: 'done',
    results: JSON.stringify(results),
  });
{'fraction_non_alphanumeric': 0.15857247976453273, 'fraction_numerical': 0.0, 'mean_word_length': 2.8295774647887324, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6476576', 'n_tokens_mistral': 923, 'n_tokens_neox': 901, 'n_words': 203}