1

I'm calling a polling function like so:

PollServer.getData(id)

It's a process that takes about 10 seconds and has a 30-second timeout. How can I assign in to a variable that'll wait for the timeout or the successful response (which is an array)?

This causes it to set the value immediately instead of waiting for the process:

var response = PollServer.getData(id)

And so does this:

fetch(PollServer.getData(id))
  .then(function(response) {
    console.log(`Final: ${response}`)
}).catch(function(error) {
  console.log(`Super error: ${error}`)
})

What am I missing?

Here's the PollServer class:

export class PollServer {
  static getData(id) {
    const token = process.env.SERVER_TOKEN,
          endpoint = `/documents/${id}`,
          request = `${endpoint}/request?token=${token}`,
          result = `${endpoint}/result?token=${token}`

    fetch(request, { method: 'get' })
      .then(function(response) {
        PollServer.poll(function() {
          return fetch(result, { method: 'get' })
        }, 30000, 500)
      .then(function(response) {
        console.log(`Finished: ${JSON.stringify(response)}`)
        return [response.low_url, response.high_url]
      })
    }).catch(function(error) {
      console.log(`Error: ${error}`)
    })
  }

  static poll(fn, timeout, interval) {
    var endTime = Number(new Date()) + (timeout || 2000)
    interval = interval || 100

    var checkCondition = function(resolve, reject) {
      var fetchCall = fn()
      fetchCall.then(function(response) {
        return response.json()
      }).then(function(response) {
        if (JSON.stringify(response.created) == 'true') {
          resolve(response)
        } else if (Number(new Date()) < endTime) {
          setTimeout(checkCondition, interval, resolve, reject)
        } else {
          reject(new Error(`Timeout: ${fn} with ${arguments}`))
        }
      })
    }
    return new Promise(checkCondition)
  }
}
t56k
  • 6,769
  • 9
  • 52
  • 115
  • 1
    What exactly is `PollServer`? What sort of object does it return? – CertainPerformance Apr 02 '18 at 03:26
  • Maybe duplicate with https://stackoverflow.com/questions/31710768/how-can-i-fetch-an-array-of-urls-with-promise-all – Tan Duong Apr 02 '18 at 03:27
  • you seem to think that `PollServer.getData(id)` immediately returns an object that can be used by `fetch` ... if this isn't the case, then your fetch will fail – Jaromanda X Apr 02 '18 at 03:28
  • @CertainPerformance Updated the question. – t56k Apr 02 '18 at 03:29
  • ahh, I see .. `PollServer.getData(id)` doesn't return anything, so, can not be used like that at all - and the first `.then` inside `getData` also returns nothing, so, all in all, your issue is failure to return anything at all, let alone anything useful – Jaromanda X Apr 02 '18 at 03:29
  • @JaromandaX It does eventually with the `return [response.low_url, response.high_url]`. – t56k Apr 02 '18 at 03:31
  • no. not that `.then` ... the previous one. I clearly stated the **first** .then doesn't return anything – Jaromanda X Apr 02 '18 at 03:33
  • @JaromandaX You're right, my bad. – t56k Apr 02 '18 at 03:37
  • still .. `fetch(PollServer.getData(id))` will be `fetch(promiseObject)` which wont work ... `PollServer.getData(id).then(x => fetch(x))` instead - as long as `x` is something usable by fetch of course – Jaromanda X Apr 02 '18 at 03:41

1 Answers1

0

So in light of this post I've solved my issue, which essentially is to return a Promise, a la:

static getData(id) {
    const token = process.env.SERVER_TOKEN,
          endpoint = `/documents/${id}`,
          request = `${endpoint}/request?token=${token}`,
          result = `${endpoint}/result?token=${token}`

    return new Promise(function(resolve, reject) { // see here for what to do
      fetch(request, { method: 'get' })
        .then(function(response) {
          PollServer.poll(function() {
            return fetch(result, { method: 'get' })
          }, 30000, 500)
        .then(function(response) {
          resolve(response) // and resolve it here
        })
      }).catch(function(error) {
        reject(new Error(`Error: ${error}`))
      })
    })
}
t56k
  • 6,769
  • 9
  • 52
  • 115