RSVP - Processing timeouts with promises

I am using ember.js and RSVP.

From what I see, there is nothing that handles the timeout from an asynchronous call.

My thinking is to wrap the permission handler using the decorator pattern to wrap the permission handler in some code that will be the time of the call and reject the call if a timeout occurs.

This sounds like a good idea or there is some built-in timeout support that I missed in RSVP.

+4
source share
2 answers

You can do this, but it should probably be handled by what the async operation does. If you are using jQuery ajax then:

$.ajax({
  //...
  timeout: 1000 * 10 // 10 seconds
  //...
})

, .

+1

, jQuery, , Promise.race, .

/**
 * @param {number} msWait
 * @param {string} error - error message
 * @return {Promise}
 */
const promiseTimeout = (msWait, error) => {
  return new Promise((resolve, reject) => {
    setTimeout(() => reject(new TimeoutError(error)), msWait)
  })
}

// Run tasks with timeout error
Promise.race([
  Android.detector(),
  IOS.detector(),
  promiseTimeout(settings.platformDetectionTimeout, 'Can\'t detect your platform')
])
+1

Source: https://habr.com/ru/post/1533702/


All Articles