Loadable-components: [Feature Request] Integrating Delay and Timeout features with this library

Created on 5 Feb 2018  路  7Comments  路  Source: gregberge/loadable-components

Thank you for this great library. I have integrated it with my React starter with painless. I like your design philosophy. If you can integrate Delay and Timeout into this library I think it would be great. These features are basic necessary for async component 馃憤

Most helpful comment

const TimeoutComponent = loadable(pDelay(pTimeout(() => import('./MyComponent'), 3000), 200))

馃槈

If you like functional programming and composition you can slightly change it.

const delay = ms => new Promise(resolve => setTimeout(resolve), ms)

const pTimeout = ms => async promise =>
  Promise.race([
    promise,
    async () => {
      await delay(ms)
      throw new Error('Timeout')
    },
  ])

const pDelay = ms => async promise => {
  await wait(ms)
  return promise
}

const DelayedComponent = loadable(compose(
  pDelay(200),
  pTimeout(200),
)(() => import('./MyComponent')))

All 7 comments

Thanks! delay and timeout are already supported. I prefer to make it optional and to keep it as small as possible. It is documented in the readme for delay and for timeout.

@neoziro If user can use delay and timeout without external dependency it would be great.

I understand but I prefer to keep it separated, if you want to avoid additional dependencies, you can code it quickly:

const delay = ms => new Promise(resolve => setTimeout(resolve), ms)

const pTimeout = async (promise, ms) =>
  Promise.race([
    promise,
    async () => {
      await delay(ms)
      throw new Error('Timeout')
    },
  ])

const pDelay = async (promise, ms) => {
  await wait(ms)
  return promise
}

const DelayedComponent = loadable(pDelay(() => import('./MyComponent'), 200))
const TimeoutComponent = loadable(pTimeout(() => import('./MyComponent'), 3000))

@neoziro Thank you for your response. Should we need to handle the case of delay + timeout?

const TimeoutComponent = loadable(pDelay(pTimeout(() => import('./MyComponent'), 3000), 200))

馃槈

If you like functional programming and composition you can slightly change it.

const delay = ms => new Promise(resolve => setTimeout(resolve), ms)

const pTimeout = ms => async promise =>
  Promise.race([
    promise,
    async () => {
      await delay(ms)
      throw new Error('Timeout')
    },
  ])

const pDelay = ms => async promise => {
  await wait(ms)
  return promise
}

const DelayedComponent = loadable(compose(
  pDelay(200),
  pTimeout(200),
)(() => import('./MyComponent')))

@neoziro Thanks. It really helpful ;)

@neoziro If you can add the "timeout + delay" as a part of your README. It would be great 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bitttttten picture bitttttten  路  8Comments

sinwailam193 picture sinwailam193  路  6Comments

lauterry picture lauterry  路  6Comments

gregberge picture gregberge  路  7Comments

hemmxwxsoo picture hemmxwxsoo  路  4Comments