0.0.6 • Published 4 years ago

@pskl/promise-interceptor-handler v0.0.6

Weekly downloads
16
License
ISC
Repository
github
Last release
4 years ago

Promise Interceptor Handler

Handle promises set when errored and repeat failed sequence after refresh function

Dirty example

import PromiseInterceptorHandler from '@pskl/promise-interceptor-handler'

const interceptor = new PromiseInterceptorHandler(
  () => new Promise(r => setTimeout(r, 10000)),
  () => true
)

const reqs: any[] = []

const fn = (arg: any) => { 
  return new Promise((rs, rj) => {
    setTimeout(() => {
        if(arg % 4 == 0 && !reqs.includes(arg)) {
            reqs.push(arg);
            (rj({ status: false }), console.log('fail', arg))
        } else {
          (rs(arg), console.log('success', arg))
        }
    }, Math.random() * 2000)
  })
}

Promise.all(
  Array.from({ length: 17 }).map((_, i) => interceptor.exec(fn.bind(null, i)))
).then(data => console.log(data))
.catch(() => console.log('&&&&&&&&&&&&&'))

setTimeout(() => {
  Promise.all(
    Array.from({ length: 17 }).map((_, i) => interceptor.exec(fn.bind(null, i * 2)).then(( data ) => (console.log({i, data}), data)))
    ).then(data => console.log(data))
  .catch(() => console.log('&&&&&&&&&&&&&'))
}, 7000)

setTimeout(() => {
  Promise.all(
    Array.from({ length: 17 }).map((_, i) => interceptor.exec(fn.bind(null, i * 3)))
  ).then(data => console.log(data))
  .catch(() => console.log('&&&&&&&&&&&&&'))
}, 15000)
0.0.3

4 years ago

0.0.2

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.6

4 years ago

0.0.1

4 years ago

0.0.0

4 years ago