Tuesday 23 May 2017

Return value from cron using node-schedule

I'm a beginner in javascript/node. I need to refresh some variable every n second. I try to use cron but always get previous value instead of new value. Here's the example:

var Schedule = require('node-schedule')
var Request = require('sync-request')

const URL = 'http://example.com'

function getData () {
  var response = Request('GET', URL)
  var body = response.getBody().toString('utf8')

  return body
}

function worldstate () {
  var state = getData()

  Schedule.scheduleJob('*/10 * * * * *', function () {
    state = getData()
  })

  return state
}

module.exports = {
  worldstate
}

My intent is to export the variable/function so it can be used in another components.



via S-audade

No comments:

Post a Comment