0

How can I get the ordered execution of asynchronous functions mentioned below

file one.js

const request = require("request")

makerequest = function() {
    let value = false;

    request("http://www.google.com", function(err, response, body) {
        if(err)
            console.log("Error", err)
        else if(response && body)
        {
            console.log("response is valid")
            console.log("body is valid")
        }
        value = true
    })
    return value
}

exports.makerequest = makerequest

file two.js

const one = require("./one")

post("passed string", function() {
    console.log("one")
    result = one.makerequest()
    console.log("result", result)
    console.log("two")
})

function post(str, callback) {
    console.log(str)
    callback()
}

This is the result that I'm getting

passed string
one
result false
two
response is valid
body is valid

This is the result that I want to get

passed string
one
response is valid
body is valid
result true
two

I tried to use async/await as well but they also didn't work out.

Kartik Chauhan
  • 2,779
  • 5
  • 28
  • 39
  • 2
    Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – tkausl Nov 05 '18 at 18:49
  • I tried to follow the answer but still not getting it. Would you please help in making me understand how can I achieve the desired output? I'm aware of the asynchronous nature of javascript but I don't know how to use async/await or promises in this situation. – Kartik Chauhan Nov 05 '18 at 19:01
  • Sorry but this is too broad, since the code you've given is for a hypothetical example and you're trying to understand how async code works. I recommend you go learn about Promises before tackling async/await. Promises are a bit easier to understand are are compatible with async/await later once you're ready for that. – jered Nov 05 '18 at 19:10
  • @jered Can you answer the question that I asked in my answer? – Kartik Chauhan Nov 05 '18 at 19:26

1 Answers1

1

This is what I did to get the desired output

one.js

const request = require("request")

makerequest = function() {
    let value = false;

    return new Promise(function(resolve, reject) {
        request("http://www.google.com", function(err, response, body) {
            if(err)
                console.log("Error", err)
            else if(response && body)
            {
                console.log("response is valid")
                console.log("body is valid")
            }
                value = true
                resolve(value)
        })
    })
}

module.exports = makerequest

two.js

const makerequest = require("./one")

post("passed string", async function() {
    console.log("one")
    result = await makerequest()
    console.log("result", result)
    console.log("two")
})

function post(str, callback) {
    console.log(str)
    callback()
}

However, there's one thing that I still didn't get. When I write resolve(value) outside request function call, the promise doesn't behave in the desired manner. What's the explanation for this? It's still inside Promise.

Kartik Chauhan
  • 2,779
  • 5
  • 28
  • 39
  • 1
    Because you should only call `resolve()` with the final value once it's actually available. If you call it outside of the `request()` call, you would synchronously fire the `resolve()` call before the `request()` had actually finished its network request and retrieved the data. – jered Nov 05 '18 at 19:32
  • 1
    Good job solving the problem yourself though :) – jered Nov 05 '18 at 19:33