nv-facutil-promise v1.0.34
nv-facutil-promise
- nv-facutil-promise is a simple-util for promise
install
- npm install nv-facutil-promise
usage
example
promise,resolve,reject
const {psj,ka,TmoutError} = require("nv-facutil-promise");
var [p,resolve,reject] = psj();
> var [p,resolve,reject] = psj();
> p
Promise { <pending> }
> resolve
[Function (anonymous)]
> reject
[Function (anonymous)]
>
> resolve("xxxxx")
>
> p
Promise { 'xxxxx' }
>
keepalive
renew renew(auto_start=true) auto-start: if-renew-in-paused-stete ,auto-start=false will keep paused state
var p = ka(30000,(err)=>console.log(err))
> p
Promise { <pending> }
> p.lefted_
23401
> p.lefted_
22019
>
> p.renew()
true
> p.lefted_
25636
> p.renew()
true
> p.lefted_
28612
>
> var p = ka(30000,(err)=>console.log(err))
undefined
> p.pause()
undefined
> p.lefted_
25718
> p.renew(false)
true
> p.is_paused()
true
> p.lefted_
30000
>
p.continue()
timeout
TmoutError: 30000
at Timeout.<anonymous> (/opt/JS/NV5_/nv-facutil_/pkgs/nv-facutil-promise/index.js:283:17)
at listOnTimeout (node:internal/timers:556:17)
at processTimers (node:internal/timers:499:7)
never
> var p = ka(3000,(err)=>console.log(err))
> p.lefted_
1326
> p.never()
true
> p
Promise { <pending> }
> p.passed_
10284
> p.passed_
11817
> p.passed_
13089
> p
Promise { <pending> }
> p.lefted_
Infinity
>
pause /continue
> var p = ka(30000,(err)=>console.log(err))
> p.lefted_
27414
> p.lefted_
25907
> p.lefted_
24696
>
> p.pause()
> p.lefted_
18682
> p.lefted_
18682
> p.lefted_
18682
> p.is_paused()
true
> p.passed_
11318
> p.cache_
TimeRangeCache(1) [ [ 1627208913361, 1627208924679 ] ]
>
> p.continue()
> p.lefted_
11573
> p.lefted_
9011
> p.lefted_
7494
> TmoutError: 30000
at Timeout.<anonymous> (/opt/JS/NV5_/nv-facutil_/pkgs/nv-facutil-promise/index.js:283:17)
at listOnTimeout (node:internal/timers:556:17)
at processTimers (node:internal/timers:499:7)
> p.lefted_
-1
> p.passed_
30007
>
postpone for current round
var p = ka(3000,(err)=>console.log(err))
p.postpone(15000)
> var p = ka(3000,(err)=>console.log(err))
undefined
> p.postpone(15000)
true
> p.passed_
10137
> p.passed_
12841
> TmoutError: 18000
at Timeout.<anonymous> (/opt/JS/NV5_/nv-facutil_/pkgs/nv-facutil-promise/index.js:283:17)
at listOnTimeout (node:internal/timers:556:17)
at processTimers (node:internal/timers:499:7)
> p.passed_
18021
>
ahead for current round
> var p = ka(30000,(err)=>console.log(err))
> p.ahead(15000)
true
> p.cache_TmoutError: 15000
at Timeout.<anonymous> (/opt/JS/NV5_/nv-facutil_/pkgs/nv-facutil-promise/index.js:283:17)
at listOnTimeout (node:internal/timers:556:17)
at processTimers (node:internal/timers:499:7)
TimeRangeCache(1) [ [ 1627210458138, 1627210473141 ] ]
> p.passed_
15003
>
immediate timeout
> var p = ka(30000,(err)=>console.log(err))
>
> p
Promise { <pending> }
> p.immediate()
> TmoutError: 7399
at Promise.p.immediate (/opt/JS/NV5_/nv-facutil_/pkgs/nv-facutil-promise/index.js:325:17)
at REPL5:1:3
at Script.runInThisContext (node:vm:131:12)
at REPLServer.defaultEval (node:repl:514:29)
at bound (node:domain:416:15)
at REPLServer.runBound [as eval] (node:domain:427:12)
at REPLServer.onLine (node:repl:836:10)
at REPLServer.emit (node:events:390:22)
at REPLServer.EventEmitter.emit (node:domain:470:12)
at REPLServer.Interface._onLine (node:readline:418:10)
> p.lefted_
-1
> p.passed_
7400
> p.cache_
TimeRangeCache(1) [ [ 1627209498560, 1627209505960 ] ]
>
cancel
> var p = ka(30000,(err)=>console.log(err))
undefined
> p.lefted_
26159
> p.lefted_
23124
> p.cancel()
undefined
> p
Promise { 'canceled' }
> p.lefted_
-1
> p.passed_
16958
>
change(ntmout,diff=false) similar as posphone and ahead,but will change tmout and affect renew
> var p = ka(30000,(err)=>console.log(err))
> p.change(10000)
true
> p.tmout_TmoutError: 10000
at Timeout.<anonymous> (/opt/JS/NV5_/nv-facutil_/pkgs/nv-facutil-promise/index.js:283:17)
at listOnTimeout (node:internal/timers:556:17)
at processTimers (node:internal/timers:499:7)
10000
>
> var p = ka(30000,(err)=>console.log(err))
> p.change(10000,true)
true
> p.tmout_
40000
>
> p.lefted_
3769
> p.lefted_
TmoutError: 40000
at Timeout.<anonymous> (/opt/JS/NV5_/nv-facutil_/pkgs/nv-facutil-promise/index.js:283:17)
at listOnTimeout (node:internal/timers:556:17)
at processTimers (node:internal/timers:499:7)
-1
>
timeout
tmout = 5000
var [p,resolve,reject] = psj(tmout);
time-range-cache
> const {TimeRangeCache} = require("nv-facutil-promise")
> var cache = new TimeRangeCache()
> cache
TimeRangeCache(0) []
> cache.pusht()
undefined
> cache
TimeRangeCache(1) [ [ 1627203152173, null ] ]
> cache.pusht()
> cache
TimeRangeCache(1) [ [ 1627203152173, 1627203156663 ] ]
> cache.pusht()
> cache
TimeRangeCache(2) [
[ 1627203152173, 1627203156663 ],
[ 1627203161607, null ]
]
> cache.pusht()
> cache
TimeRangeCache(2) [
[ 1627203152173, 1627203156663 ],
[ 1627203161607, 1627203165513 ]
]
> cache.passed_
8396
> cache.lst_
[ 1627203161607, 1627203165513 ]
> 1627203156663 - 1627203152173 + 1627203165513 - 1627203161607
8396
> cache.clear()
TimeRangeCache(0) []
> cache
TimeRangeCache(0) []
>
others
const fac_prom = require("nv-facutil-promise");
//the name is not accurte:
// Promises have three possible mutually exclusive states:
// fulfilled, rejected, and pending
// Promises have two possible mutually exclusive fates:
// resolved, and unresolved.
//but for convinient
// dont use fates
// just use states: in states resolved === fullfilled
> fac_prom.STATES_MD
{
'0': 'pending',
'1': 'fulfilled',
'2': 'rejected',
pending: 0,
fulfilled: 1,
rejected: 2
}
>
> fac_prom.FATES_MD
{
resolved: [ 'fulfilled', 'rejected' ],
unresolved: [ 'pending' ],
fulfilled: 'resolved',
rejected: 'resolved',
pending: 'unresolved'
}
>
//creat_manual_promise
var p = fac_prom.creat_manual_promise()
> p
Promise { <pending> }
> fac_prom.get_promise_state_in_nodejs(p)
'pending'
>
> fac_prom.get_promise_fate_in_nodejs(p)
'unresolved'
>
> var state = fac_prom.get_promise_state_container(p)
undefined
> state
{ state: 'pending' }
>
> var fate = fac_prom.get_promise_fate_container(p)
undefined
> fate
{ fate: 'unresolved' }
>
> p.$resolve(1000)
undefined
> p
Promise { 1000 }
>
> state
{ state: 'fulfilled' }
> fate
{ fate: 'resolved' }
>
> fac_prom.get_promise_state_in_nodejs(p)
'fulfilled'
>
//creat_promise
function func(rs,rj,...args) {
let v = args[0];
if(v<0) {
rj("value must >=0")
} else if(v<=100) {
rs(v)
} else {
rj("value must <=100")
}
}
var p = fac_prom.creat_promise(func,999)
> p
Promise { <rejected> 'value must <=100' }
>
//creat_setmout(func,tmout,...args)
var p = fac_prom.creat_setmout(func,5000,90)
> p
Promise { <pending> }
> p
Promise { <pending> }
> p
Promise { 90 }
>
//
> var p = fac_prom.creat_setmout_resolve('a',5000)
undefined
> p
Promise { <pending> }
> p
Promise { <pending> }
> p
Promise { <pending> }
> p
Promise { 'a' }
>
var p = fac_prom.creat_setmout_reject('a',5000)
> p
Promise { <rejected> 'a' }
>
//pthen,pcatch,pfinally
//
var p = fac_prom.creat_setmout(func,5000,90)
var d = fac_prom.get_then_catch_finally(p)
> d.then(r=>{console.log(r)})
Promise { <pending> }
> 90
> fac_prom.pthen(p)(r=>{console.log(r)})
Promise { <pending> }
> 90
//shared
//then,catch, use the same callback
var p = fac_prom.creat_setmout(func,5000,90)
fac_prom.shared_then_catch(p,r=>{console.log(r,"all-in-one")})
> 90 all-in-one
> p
Promise { 90 }
>
var p = fac_prom.creat_setmout(func,5000,101)
fac_prom.shared_then_catch(p,r=>{console.log(r,"all-in-one")})
> value must <=100 all-in-one
> p
Promise { <rejected> 'value must <=100' }
>
set_interval
var{set_interval} = require("./index")
function tst(a,b) {
let r = `${new Date()}:${b}`
console.log(r);
return(r)
}
var tmout =[1000,5000,1000]
var {rslts,newClearInterval} = set_interval(tst,tmout,10,20)
/*
> Wed Sep 08 2021 07:56:55 GMT+0000 (Coordinated Universal Time):20
Wed Sep 08 2021 07:57:00 GMT+0000 (Coordinated Universal Time):20
Wed Sep 08 2021 07:57:01 GMT+0000 (Coordinated Universal Time):20
Wed Sep 08 2021 07:57:02 GMT+0000 (Coordinated Universal Time):20
Wed Sep 08 2021 07:57:07 GMT+0000 (Coordinated Universal Time):20
Wed Sep 08 2021 07:57:08 GMT+0000 (Coordinated Universal Time):20
Wed Sep 08 2021 07:57:09 GMT+0000 (Coordinated Universal Time):20
Wed Sep 08 2021 07:57:14 GMT+0000 (Coordinated Universal Time):20
Wed Sep 08 2021 07:57:15 GMT+0000 (Coordinated Universal Time):20
Wed Sep 08 2021 07:57:16 GMT+0000 (Coordinated Universal Time):20
Wed Sep 08 2021 07:57:21 GMT+0000 (Coordinated Universal Time):20
Wed Sep 08 2021 07:57:22 GMT+0000 (Coordinated Universal Time):20
Wed Sep 08 2021 07:57:23 GMT+0000 (Coordinated Universal Time):20
Wed Sep 08 2021 07:57:28 GMT+0000 (Coordinated Universal Time):20
Wed Sep 08 2021 07:57:29 GMT+0000 (Coordinated Universal Time):20
*/
newClearInterval()
> rslts
[
'Wed Sep 08 2021 07:56:55 GMT+0000 (Coordinated Universal Time):20',
'Wed Sep 08 2021 07:57:00 GMT+0000 (Coordinated Universal Time):20',
'Wed Sep 08 2021 07:57:01 GMT+0000 (Coordinated Universal Time):20',
'Wed Sep 08 2021 07:57:02 GMT+0000 (Coordinated Universal Time):20',
'Wed Sep 08 2021 07:57:07 GMT+0000 (Coordinated Universal Time):20',
'Wed Sep 08 2021 07:57:08 GMT+0000 (Coordinated Universal Time):20',
'Wed Sep 08 2021 07:57:09 GMT+0000 (Coordinated Universal Time):20',
'Wed Sep 08 2021 07:57:14 GMT+0000 (Coordinated Universal Time):20',
'Wed Sep 08 2021 07:57:15 GMT+0000 (Coordinated Universal Time):20',
'Wed Sep 08 2021 07:57:16 GMT+0000 (Coordinated Universal Time):20',
'Wed Sep 08 2021 07:57:21 GMT+0000 (Coordinated Universal Time):20',
'Wed Sep 08 2021 07:57:22 GMT+0000 (Coordinated Universal Time):20',
'Wed Sep 08 2021 07:57:23 GMT+0000 (Coordinated Universal Time):20',
'Wed Sep 08 2021 07:57:28 GMT+0000 (Coordinated Universal Time):20',
'Wed Sep 08 2021 07:57:29 GMT+0000 (Coordinated Universal Time):20'
]
>
Capability
- Capability is same as creat_manual_promise, both for compatibility-with-some-libs
- normally useless , use _psj() OR psj()
> Capability
[class Capability] {
is_reset_signal: [Function (anonymous)],
is_pending_value: [Function (anonymous)]
}
>
> var c = new Capability()
c.p_ //Promise, for await
c.resolve(data) //manual-resolve(data)
> c.p_
Promise { <pending> }
>
> c.resolve("OK")
undefined
> c.p_
Promise { 'OK' }
>
c.reject(reason) //manual-reject(reason)
c.random_settle(val) // random-settle, resolve-or-reject, this is for testing-code
c.reset(as_exception=false) // reset, will resolve-a-special-symbol to the old-promise
// use Capability.is_reset_signal to check
// AND refresh c.p_
var c = new Capability()
var old_promise = c.p_;
> old_promise
Promise { <pending> }
>
c.reset()
> old_promise
Promise { Symbol(reset) }
>
> var r = await old_promise
undefined
> Capability.is_reset_signal(r)
true
>
> c.p_
Promise { <pending> }
>
c.rslt_ //get resolved data
c.exception_ //get rejected reason
c.settled_ //get resolved data(if resolved) OR rejected reason(if rejected)
//OR a-special-symbol use Capability.is_pending_value to check
c.is_pending
c.is_rejected
c.is_resolved
c.is_fulfilled //same as is_resolved, for compatible with some libs
c.is_settled
c.is_fate_resolved //resolved OR rejected for compatible with some libs
c.is_fate_unresolved //pending for compatible with some libs
replace
- creat a new promise based-on the old-promise
- rplc(old,new_rslt,new_err)
- rplc_rslt(old,new_rslt)
- rplc_err(old,new_err)
var p = Promise.resolve('old-rslt')
var np = rplc_rslt(p,'new-rslt')
/*
> np
Promise {
'new-rslt',
[Symbol(async_id_symbol)]: 325,
[Symbol(trigger_async_id_symbol)]: 5,
[Symbol(destroyed)]: { destroyed: false }
}
>
*/
function tst() {
var p = new Promise(
(rs,rj)=> {
setTimeout(
()=> {
if(Math.random()>0.5) {
rs("old-rslt")
} else {
rj("old-err")
}
},
10000
)
}
);
return(p)
}
var p = tst()
var np = rplc(p,'new-rslt','new-err')
/*
> p
Promise {
'old-rslt',
[Symbol(async_id_symbol)]: 883,
[Symbol(trigger_async_id_symbol)]: 5,
[Symbol(destroyed)]: { destroyed: false }
}
>
> np
Promise {
'new-rslt',
[Symbol(async_id_symbol)]: 890,
[Symbol(trigger_async_id_symbol)]: 5,
[Symbol(destroyed)]: { destroyed: false }
}
>
*/
/*
> p
Promise {
<rejected> 'old-err',
[Symbol(async_id_symbol)]: 949,
[Symbol(trigger_async_id_symbol)]: 5,
[Symbol(destroyed)]: { destroyed: false }
}
> np
Promise {
<rejected> 'new-err',
[Symbol(async_id_symbol)]: 956,
[Symbol(trigger_async_id_symbol)]: 5,
[Symbol(destroyed)]: { destroyed: false }
}
>
*/
wait_until
> new Date()
2022-03-02T05:08:52.901Z
> await x.wait_until(new Date("2022-03-02T05:09:03.556Z"))
Result
Result
var none = undefined
> var p = new Result(none)
> p
Result [Promise] {
<pending>,
[Symbol(async_id_symbol)]: 108,
[Symbol(trigger_async_id_symbol)]: 5,
[Symbol(destroyed)]: { destroyed: false }
}
> p.is_pending()
true
>
> p.is_pending()
true
> p.ok('data')
> p
Result [Promise] {
'data',
[Symbol(async_id_symbol)]: 108,
[Symbol(trigger_async_id_symbol)]: 5,
[Symbol(destroyed)]: { destroyed: false }
}
> p.is_settled()
true
> p.is_resolved()
true
>
> p.empty_
undefined
> p.rslt_
'data'
>
> var p = new Result()
undefined
> p.err(new Error())
undefined
> p.ok(999)
undefined
> p
Result [Promise] {
<rejected> Error
at REPL85:1:7
at Script.runInThisContext (node:vm:129:12)
at REPLServer.defaultEval (node:repl:562:29)
at bound (node:domain:421:15)
at REPLServer.runBound [as eval] (node:domain:432:12)
at REPLServer.onLine (node:repl:889:10)
at REPLServer.emit (node:events:402:35)
at REPLServer.emit (node:domain:475:12)
at REPLServer.Interface._onLine (node:readline:487:10)
at REPLServer.Interface._line (node:readline:864:8),
[Symbol(async_id_symbol)]: 505,
[Symbol(trigger_async_id_symbol)]: 5,
[Symbol(destroyed)]: { destroyed: false }
}
>
> var p = new Result()
> p.err(new Error())
> p
Result [Promise] {
<rejected> Error
at REPL85:1:7
at Script.runInThisContext (node:vm:129:12)
at REPLServer.defaultEval (node:repl:562:29)
at bound (node:domain:421:15)
at REPLServer.runBound [as eval] (node:domain:432:12)
at REPLServer.onLine (node:repl:889:10)
at REPLServer.emit (node:events:402:35)
at REPLServer.emit (node:domain:475:12)
at REPLServer.Interface._onLine (node:readline:487:10)
at REPLServer.Interface._line (node:readline:864:8),
[Symbol(async_id_symbol)]: 505,
[Symbol(trigger_async_id_symbol)]: 5,
[Symbol(destroyed)]: { destroyed: false }
}
> p.exception_
Error
at REPL85:1:7
at Script.runInThisContext (node:vm:129:12)
at REPLServer.defaultEval (node:repl:562:29)
at bound (node:domain:421:15)
at REPLServer.runBound [as eval] (node:domain:432:12)
at REPLServer.onLine (node:repl:889:10)
at REPLServer.emit (node:events:402:35)
at REPLServer.emit (node:domain:475:12)
at REPLServer.Interface._onLine (node:readline:487:10)
at REPLServer.Interface._line (node:readline:864:8)
> p.is_pending()
false
> p.is_settled()
true
> p.is_rejected()
true
>
p.catch p.constructor p.empty_ p.err
p.exception_ p.finally p.is_pending p.is_rejected
p.is_resolved p.is_settled p.ok p.rslt_
p.then
result
- same as Result, it is a function
- and the only different is result did NOT extends Promise. it use the original promise
> var p = result(undefined)
> p
Promise {
<pending>,
ok: [Function (anonymous)],
err: [Function (anonymous)],
is_pending: [Function (anonymous)],
is_settled: [Function (anonymous)],
is_resolved: [Function (anonymous)],
is_rejected: [Function (anonymous)],
get_rslt: [Function (anonymous)],
get_exception: [Function (anonymous)],
}
>
API
- fac_prom.TmoutError
- fac_prom.MAX_NODEJS_TMOUT
- fac_prom.psj(tmout=undefined):p:Promise,resolve:Function,reject:Function
fac_prom.ka(keepalive_tmout,tmout_error_handler=(err)=>{},cancel_handler=(r)=>{})
fac_prom.newSetTimeout(f,tmout,...params) //tmout nolimit
- fac_prom.newClearTimeout(ids)
- fac_prom.wait(delay) //(delay)=>undefined dly nolimit
fac_prom.wait_until(date)
fac_prom.set_interval(f,tmout,...params) //rtrn: {rslts:Array,newClearInterval:()=>{}}
fac_prom.set_interval_without_rslts(f,tmout,...params) //rtrn: newClearInterval:()=>{}
fac_prom.get_promise_state_container(p)
- fac_prom.get_promise_fate_container(p)
- fac_prom.get_promise_state_in_nodejs(p)
fac_prom.get_promise_fate_in_nodejs(p)
fac_prom.creat_manual_promise() //rtrn p:{$resolve,$reject}
- fac_prom.creat_promise(func,...args) //(func:(rs,rj,...params),...params)
- fac_prom.creat_setmout_resolve(val,tmout,...args) //(val,tmout,...params)
- fac_prom.creat_setmout_reject(val,tmout,...args) //(val,tmout,...params)
- fac_prom.creat_setmout(func,tmout,...args) //(tmout,f,...args) f(...args) must-rtrn-a-promise
- fac_prom.creat_setmout_at(func,date,...args) //(date,f,...args) f(...args) must-rtrn-a-promise
- fac_prom.pthen(p)
- fac_prom.pcatch(p)
- fac_prom.pfinally(p)
- fac_prom.get_then_catch_finally(p)
- fac_prom.shared_exec(p,func,...names)
- fac_prom.shared_then_catch(p,func)
- fac_prom.shared_then_finally(p,func)
- fac_prom.shared_catch_finally(p,func)
- fac_prom.shared_then_catch_finally(p,func)
fac_prom.Capability
fac_prom.Result
- fac_prom.result
LICENSE
- ISC
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago