0
var https = require('https');
async function getOrderBook(){
  var options = {
    host: 'api.bybit.com',
    port: 443,
    path: '/v2/public/orderBook/L2?symbol=BTCUSD',
    method: 'GET'
  };

  https.request(options, function(res) {
    res.on('data', function (chunk) {
      //console.log(chunk);
      return chunk;
    });
  }).end();
}

console.log(getOrderBook())

The following logs

Promise { undefined }

in the console. However, I can log it fine from inside the function. How to make it so it waits for the function to resolve?

2
  • Are you missing a return statement before http.request(..)? Commented Jun 13, 2020 at 7:01
  • I get a very long message when I do so but its not the expected response Commented Jun 13, 2020 at 7:10

2 Answers 2

1

https.request() takes a callback function. That's the function you pass in the second argument. When you return inside the function you are returning to the caller of that function, not getOrderBook(). Since there are no promises anywhere async/await is not really useful. You could wrap the request in a promise and return that, or you can pass a callback to getOrderBook() and when the request is finished call it with the data. For example:

var https = require('https');

function getOrderBook(cb) {
  var options = {
    host: 'api.bybit.com',
    port: 443,
    path: '/v2/public/orderBook/L2?symbol=BTCUSD',
    method: 'GET'
  };


  https.request(options, function(res) {
    let data = ''
    res.on('data', function(chunk) {
      data += chunk;
    });
    res.on('end', function() {
      // All done, call the callback
      cb(JSON.parse(data))
    })
  }).end();
}

// pass a callback that tells it what to do with the data
getOrderBook((data) => console.log(data.result))

Alternatively you can use a library like axios that has native promise support.

Sign up to request clarification or add additional context in comments.

4 Comments

could you save data.result in a variable?
You can, but it needs to be saved to a variable after the request is returned. The way javascript works is that it runs all your code before the request returns. So you need to deal with the return values in the a callback or a function you call from the callback. This takes a while to get used to with node — but it eventually feels natural..
how should i approach using data.result for computations then?
You write a function to do the computations, then call it with the result. getOrderBook((data) => doComputations(data.result)). Typically in node, not much happens in the main body of the script — all the action is in functions because you can control when they get called.
0

You can put await before you call the async function, but beware you need to be in an async function to use await

https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Op%C3%A9rateurs/await

3 Comments

I still get the long response mentioned in the above comment
You need to await getOrderBook and await https.request and maybe do return (await http.request())
I still get the long unexpected message after I did everything you said, I even wrapped the console.log into an async function with no success

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.