Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

247
Visualizações
How to dynamically map an array to async or sync functions?

Suppose I have the following array:

const routes = [
    { isAsync: true },
    { isAsync: false },
    { isAsync: true },
]

How can I map this to async or sync functions? Something like the following doesn't work:

routes.map(({ isAsync }, index) =>
     [isAsync ? 'async' : ''] () => { console.log('isAsync: ', isAsync, index) })

I could something like,

routes.map(({ isAsync }, index) =>
     isAsync
          ? async () => { console.log('isAsync: ', isAsync, index) }
          : () => { console.log('isAsync: ', isAsync, index) })

But I'm wondering if it's possible to do it the way I'm trying to in the first example?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I agree with @DBS comment that your second option looks just fine. Nevertheless, for completeness, here is a solution:

const AsyncFunction = Object.getPrototypeOf(async function() {}).constructor
const routes = [{
    isAsync: true
  },
  {
    isAsync: false
  },
  {
    isAsync: true
  },
];
const funcWithParams = routes.map(({
  isAsync
}, index) => new Object({
  'func': (isAsync ? Function : AsyncFunction).apply(this, ['isAsync', 'index', `console.log('isAsync: ', isAsync, index)`]),
  'isAsync': isAsync,
  'index': index
}));
funcWithParams.forEach((funcWithParam) => funcWithParam.func(funcWithParam.isAsync, funcWithParam.index));

Explanation:

For function, Documentation says:

Every JavaScript function is actually a Function object

And almost the same for async function:

In JavaScript, every asynchronous function is actually an AsyncFunction object.

With a side note:

Note that AsyncFunction is not a global object. It can be obtained with the following code: Object.getPrototypeOf(async function(){}).constructor

Your requirement for dynamically constructing a function / an async function is possible if you call the constructor of each prototype. for this to be achieved, call it with apply.

about 4 years ago · Juan Pablo Isaza Relatório

0

const routes = [{
    isAsync: true
  },
  {
    isAsync: false
  },
  {
    isAsync: true
  },
];

const result = routes.map(function({
  isAsync
}, index) {
  return isAsync ? this.async_fn.bind(null, isAsync, index) : this.fn.bind(null, isAsync, index)
}, {
  fn: (isAsync, index) => {
    console.log('isAsync:', isAsync, index)
  },
  async_fn: async(isAsync, index) => {
    console.log('isAsync:', isAsync, index)
  },
});

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda