PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, October 14, 2022

[FIXED] how to wrap axios function and let parent to wait?

 October 14, 2022     asynchronous, axios, node.js     No comments   

Issue

here's I've my axios function wrapped within a custom function:

async function getOrder(orderId) {
    // ...

    await axios({
        method: 'post',
        url: endpoint + "/mymethod",
        data: request
    }).then(function (response) {
        var data = response.data.result;
        return data;
    }).catch(function (error) {
        return { "error": error };
    });
}

but if than i call that function:

router.get('/getOrder/:id', (req, res) => {
    let result = getOrder(req.params.id);
    res.json(result);
})

it returns nothing (since its async and don't wait the .then()).

what's the best way to wrap axios/async function and call from outside?


Solution

I think you're missing await in your codes:

router.get('/getOrder/:id', async (req, res) => {
    let result = await getOrder(req.params.id);
    res.json(result);
})

====================

[New Update] for me in API function:

async getOrder(orderId) {
    try {
      const response = await axios.post(endpoint + "/mymethod")
      return response.data
    } catch (error) {
      return { "error": error }
    }
  }

and get the result:

router.get('/getOrder/:id', async (req, res) => {
    let result = await getOrder(req.params.id);
    res.json(result);
})

===========

[New Update2] here is my sample async/await function with axios

const axios = require("axios")

async function getOrder(orderId) {
  try {
    const response = await axios.get("http://google.com")
    return response.data
  } catch (error) {
    return { "error": error }
  }
}


async function main() {
  let result = await getOrder();
  console.log(result, "@@")
}
main()

====================

[New Update3] new Promise with axios:

const axios = require("axios")

async function getOrder(orderId) {
  // try {
  //   const response = await axios.get("http://google.com")
  //   return response.data
  // } catch (error) {
  //   return { "error": error }
  // }
  return await new Promise((resolve, reject) => {
    axios({
      method: 'get',
      url: "http://google.com"
    }).then(function (response) {
      var data = response.data
      resolve(data)
    }).catch(function (error) {
      reject({ "error": error })
    });
  })
}


async function main() {
  let result = await getOrder();
  console.log(result, "@@")
}
main()


Answered By - Yuu
Answer Checked By - Mary Flores (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing