S
S
SideWest2019-07-20 18:51:27
JavaScript
SideWest, 2019-07-20 18:51:27

js. How to change external value inside a promise?

I send a request to VK in a loop, in each new request I need to specify the parameter obtained from the previous one, please tell me how to implement this?

setInterval(async function(glob_ts) {
    p1 = new Promise((resolve) => {
        request.post({
            url: server['server'],
            qs: {
                access_token: TOKEN,
                v: '5.101',
                'act': 'a_check',
                'key': server['key'],
                'ts': tsi,   // Вот это при каждом запросе должно быть новое
                'wait': 25
            }
        }, (err, response, body) => {
            JSON.parse(body)['response'] ? resolve(JSON.parse(body)['response']) : resolve(JSON.parse(body));
        });
    });
    p1.then(function(value) {
        console.log(value);
        return value
    });

    p1.then(function(value) {
        tsi = value['ts']  // Вот это не меняет значение глобальной переменной
    });

}, 1)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stockholm Syndrome, 2019-07-20
@SideWest

well, you wrote a tin, you just need the implementation of requests to the long poll?
no setIntervals are needed, a new request should be sent after the previous one is completed

function getData(ts) {
  request.post({
    url: server['server'],
    qs: {
      access_token: TOKEN,
      v: '5.101',
      act: 'a_check',
      key: server['key'],
      ts: ts,  
      wait: 25
    }
  }, (err, response, body) => {
    const data = JSON.parse(body);
    getData(data.ts);
    // handle data
  });
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question