doppis
doppis

Reputation: 69

async/promise with multiple api calls

I kind of understand the differences between callbacks, promises and async await, but I'm not quite sure how to apply this to my problem.

I stripped away a lot of the code, but basically I have an app running with an endpoint that needs to execute 3 functions (maybe I need a 4th?) and also send a "res.end()" within 3 seconds. The 3 functions are dependent on each other. Do I need to chain these functions? My main() seems completely wrong.

router.post('/', function (req, res) {

        async function deleteRule() {
            axios.delete(DeleteURL, {auth: auth, httpsAgent: agent})
            .then(response => {
                let deleteRes = response.data
                console.log(deleteRes)
            })
        }

        const list = './jsons/list.json'

        async function createRule() {
            fs.readFile(list, 'utf-8', function(err, data) {
                if (err) throw err
                var thestuff = JSON.parse(data)
            
            axios.post(CreateURL, thestuff, {auth: auth, httpsAgent: agent})
            .then(response => {
                let createRes = response.data
                console.log(createRes)
            })
        })
        }

        async function orderRule() {
            axios.put(usOrderURL, theOrder, {auth: auth, httpsAgent: agent})
            .then(response => {
                let orderRes = response.data
                console.log(orderRes)
            })
        }

        async function main() {
            const deleteListResult = await deleteRule();
            const createListResult = await createRule();
            const orderListResult = await orderRule();
        }

        main();

        // res.end must finish in 3 seconds from the initial post on the first line
        res.end() 

    })

Upvotes: 2

Views: 1949

Answers (2)

hopeless-programmer
hopeless-programmer

Reputation: 990

import fs from 'fs-extra'

const list = './jsons/list.json'

async function deleteRule() {
    const response = await axios.delete(DeleteURL, {auth: auth, httpsAgent: agent})
    const deleteRes = response.data

    console.log(deleteRes)

    return deleteRes
}
async function createRule() {
    const data = await fs.readFile(list, 'utf-8')
    const theStuff = JSON.parse(data)
    const response = await axios.post(CreateURL, theStuff, {auth: auth, httpsAgent: agent})
    const createRes = response.data
    
    console.log(createRes)

    return createRes
}
async function orderRule() {
    const response = await axios.put(usOrderURL, theOrder, {auth: auth, httpsAgent: agent})
    const orderRes = response.data
    
    console.log(orderRes)

    return orderRes
}

router.post('/', async function (req, res) {
    const deleteListResult = await deleteRule();
    const createListResult = await createRule();
    const orderListResult = await orderRule();

    // res.end must finish in 3 seconds from the initial post on the first line
    res.end() 
})

Upvotes: 1

trincot
trincot

Reputation: 350272

The then() calls return promises, but you don't do anything with them. You should either await them, or return them.

Since you declared your functions as async, make use of await in them -- that is the whole point of the async keyword:

async function deleteRule() {
    let response = await axios.delete(DeleteURL, {auth: auth, httpsAgent: agent});
    console.log(response.data);
    return response.data;
}

Make a similar change to the other two rule-functions.

Upvotes: 2

Related Questions