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

92
Visualizações
I can't store response comes from payment api

ı have been devoloing an e-commerce system but ı have problem with iyzipay payment api. I make successful request and get response from server but I can't store data comes from server. anyone help?

 let returnedData = {}

  iyzipay.payment.create(paymentRequest, function (err, result) {
    if (err) {
      return next(new CustomError("Ödeme başarısız.", 500))
    } 
     return returnedData = result
  })

  //ı can't see data here and return empty {}
  console.log(returnedData)

  // but ı can see here when ı wait 1 second
  setTimeout(() => {
    console.log(returnedData)
  }, 1000);
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Since you're calling an async function you should await for it's result. That's why your first console log doesn't print the result and ater 1 sec does, because in that time(+ the execution time for the stack) you're getting the result

The function that can be awaited:

function createPayment(paymentRequest) {
    return new Promise((resolve, reject) => {
        iyzipay.payment.create(paymentRequest, function (err, result) {
            if (err) reject(new CustomError("Ödeme başarısız.", 500));
            resolve(result);
        });
    });
}

Generally:

(async () => {
    const request = {/* whatever */};
    try {
        const res = await createPayment(request);
        console.log(res); // Now it's here
    } catch(e) { console.error(e); }
})();

You're probably in a route so the actual code would be:

app.use('/payment', async (req, res, next) => {
    try {
        const result = await createPayment(req);
        console.log(result); // Now it's here
        res.end();
    } catch(e) { next(e); }
})
about 4 years ago · Juan Pablo Isaza Relatório

0

Your first console is getting called before the request gets complete. When waited for 1 sec request is completed and response is stored in the returnData variable. So for your requirement try using promise or call a function to store the response from the create payment function itself with the response as a parameter.

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