Hello everyone!
I ran into a problem that when calling the API, its response comes after the code was processed and I get undefined in the response.
For this, I have 2 files start.js and api.js.
In start.js I call api.js and wait for a response:

const api = require("./api"); const result = api.users(); console.log(result: ${result});

In api.js, I call the test api itself:

    const request = require('request');
    module.exports = {
        users: function() {
            request.post({
                url: "https://reqres.in/api/register",
                json: true,
                body: {
                    "email": "eve.holt@reqres.in",
                    "password": "pistol"
                },
                headers: {
                    'User-Agent': 'request'
                }
            }, (err, res, data) => {
                if (err) {
                    console.log('Error: ', err);
                    return err;
                } else if (res.statusCode !== 200) {
                    console.log('Status: ', res.statusCode);
                    return `Status: ${res.statusCode}`;
                } else {
                    console.log(`Success: ${JSON.stringify(data)}`);
                    return data;
                }
            });
        },

Please tell me how to set up a call so that the command in start.js will work after the response from api.js?

console.log(result: ${result})

I am attaching my text file with information.

A request is asynchronous by default. I am not sure you can switch that off by using a setting in the post. The other option would be to use await and a Promise.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.