Git Product home page Git Product logo

Comments (7)

gregberge avatar gregberge commented on August 10, 2024 5
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')))

from loadable-components.

gregberge avatar gregberge commented on August 10, 2024 1

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))

from loadable-components.

gregberge avatar gregberge commented on August 10, 2024

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.

from loadable-components.

wellyshen avatar wellyshen commented on August 10, 2024

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

from loadable-components.

wellyshen avatar wellyshen commented on August 10, 2024

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

from loadable-components.

wellyshen avatar wellyshen commented on August 10, 2024

@neoziro Thanks. It really helpful ;)

from loadable-components.

wellyshen avatar wellyshen commented on August 10, 2024

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

from loadable-components.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.