# nv-facutil-cycle-promise

> cycle,promise,cycle

Latest version **1.0.1** (published 2021-02-01) · ISC license · 2 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install nv-facutil-cycle-promise
pnpm add nv-facutil-cycle-promise
yarn add nv-facutil-cycle-promise
bun add nv-facutil-cycle-promise
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2021-02-01 |
| First published | 2021-02-01 |
| Weekly downloads | 2 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | ihgazni2 |
| Keywords | cycle, promise, cycle-promise |

## Links

- npm: https://www.npmjs.com/package/nv-facutil-cycle-promise
- npm.io page: https://npm.io/package/nv-facutil-cycle-promise

## Dependencies (1)

- [nv-facutil-basic](https://npm.io/package/nv-facutil-basic.md) ^1.0.1

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 1.0.1 (latest) — 2021-02-01
- 1.0.0 — 2021-02-01

## README

nv-facutil-cycle-promise
========================
- nv-facutil-cycle-promise  is a simple-util for cycling promise  
- self-defined cycle-terminator
- optionally ignore catch error


install
=======
- npm install nv-facutil-cycle-promise 

usage
=====
    
    var p = new CPromise((rs,rj,[self]) => {/*....*/},[record_dict])
        record_dict 
            is a object for saving result of each round, could be get by self.args
        self : 
            self.then_counter,
            self.catch_counter,
            self.continuous_catch_counter        //continuous error 
    
    p.then_terminator = (self)=>{/*...*/}        //cycle-terminator
    p.catch_terminator = (self)=>{/*...*/}       //cycle-terminator

    p.launch();
    p.then, p.catch,p.finnally
    ////
    p.reset(record_dict);

example
-------

    const {CPromise} = require("nv-facutil-cycle-promise")

###cycling

        var p = new CPromise(
            (rs,rj,self) => {
                setTimeout(
                    ()=>{
                        console.log("triggered at",new Date()),
                        self.args.record = self.args.record +1;
                        rs(self.args.record)                       //get saving
                    },
                    5000
                )
            },
            {record:0}                                             //for saving
        );

        //set terminate condition
        p.then_terminator = (self)=>self.then_counter >=10
        p.then(r=>console.log(r))
        p.catch(err=>console.log(err))

        //launch
        p.launch()
        //


        /*
        triggered at 2021-02-01T04:05:50.077Z
        triggered at 2021-02-01T04:05:55.081Z
        triggered at 2021-02-01T04:06:00.097Z
        triggered at 2021-02-01T04:06:05.107Z
        triggered at 2021-02-01T04:06:10.122Z
        triggered at 2021-02-01T04:06:15.135Z
        triggered at 2021-02-01T04:06:20.156Z
        triggered at 2021-02-01T04:06:25.159Z
        triggered at 2021-02-01T04:06:30.179Z
        triggered at 2021-02-01T04:06:35.192Z

        { err: null, args: { record: 10 }, rslt: 10 }
        */

        > p
        {
          promise: Promise { { err: null, args: [Object], rslt: 10 } },
          args: { record: 10 }
        }
        >
        

###set cycle terminator

         var p = new CPromise(
             (rs,rj,self) => {
                 setTimeout(
                     ()=>{
                         let random = Math.random();
                         if(random>0.5) {
                             self.args.record = self.args.record +1;
                             rs(self.args.record)
                         } else {
                             rj("error happened")
                         }
                     },
                     1000
                 )
             },
             {record:0}
         );

         p.then_terminator = (self)=>self.then_counter >=10
         p.catch_terminator = (self)=>false              //ignore error
         p.then(r=>console.log(r))
         p.catch(err=>console.log(err))
         p.launch()

         { err: null, args: { record: 10 }, rslt: 10 }

         > p
         {
           promise: Promise { { err: null, args: [Object], rslt: 10 } },
           args: { record: 10 }
         }

         > p.then_counter
         10
         > p.catch_counter
         9
         >


###reset and continuous\_catch\_counter

     p.reset({record:0})

     //max allow 2 continuous error
     p.catch_terminator = (self)=>(self.continuous_catch_counter>1)
     p.then(r=>console.log(r))
     p.catch(err=>console.log(err))

     p.launch();
     /*
     { err: 'error happened', args: { record: 4 }, rslt: Symbol(empty) }

     {
       promise: Promise {
         <rejected> { err: 'error happened', args: [Object], rslt: Symbol(empty) }
       },
       args: { record: 4 }
     }
     */
     > p.continuous_catch_counter
     2
     >


###

         var p = new CPromise(
             (rs,rj,self) => {
                 setTimeout(
                     ()=>{
                         let random = Math.random();
                         if(random>0.5) {
                             self.args.record = self.args.record +1;
                             rs(self.args.record)
                         } else {
                             rj("error happened")
                         } 
                     },
                     1000
                 )
             },
             {record:0}    
         );
         
         p.then_terminator = (self)=>self.then_counter >=10
         p.catch_terminator = (self)=>false              //ignore error
         p.then(r=>console.log(r))
         p.catch(err=>console.log(err))
        
        
        function tst(p,tmout) {
            var tmoutp = new Promise(
                (rs,rj)=>{
                    setTimeout(()=>{rj("timeout")},tmout)
                }
            );
            var race = Promise.race([tmoutp,p])
             p.launch()
            return(race)
        }
        var race = tst(p,15000)
        >
        Promise { <rejected> 'timeout' }
        >
        p.reset({record:0})
        var race = tst(p,30000)
        > race
        Promise { { err: null, args: { record: 10 }, rslt: 10 } }
        >


METHODS
=======

    p.args
    p.then_counter
    p.then_terminator
    p.catch_counter
    p.continuous_catch_counter 
    p.catch_terminator 
                         
    p.launch
    p.reset
    

    p.then
    p.catch                     
    p.finally                



APIS
====
fac\_cp.CPromise(f)


LICENSE
=======
- ISC

---
_Source: https://npm.io/package/nv-facutil-cycle-promise · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
