L
L
leistolz2016-05-05 19:37:13
Node.js
leistolz, 2016-05-05 19:37:13

Synchronous function execution in node.js?

I decided to study the node, and ran into a problem: I
wrote a function

var requestSync = require('sync-request');

function getts() {
  var getts = requestSync('GET', 'https://api.vk.com/method/messages.getLongPollServer?need_pts=1&access_token='+token);
  var data = JSON.parse(getts);
  var key = (data.response.key);
  var ts = (data.response.ts);
  var pts = (data.response.pts);
}

And in theory, you first need to get the answer that will be contained in the getts variable, and then parse it, but the node does the opposite. How can I make this function work so that it first waits for a response from the server?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Super User, 2016-05-05
@leistolz

This module makes a synchronous request to the server and the node has nothing to do with it. Have you tried reading the
documentation ?
To get a response from the server, you need to call the getBody () method on getts

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question