- reflect style Promise
- only work in nodejs
- for test sig-slot feature in nvlang
- npm install nd-reflect-promise
const {
creat_one,
creat_many,
rtrn,
thrw
} = require("nd-reflect-promise");
var p = creat_one();
/*
> p
Promise {
<pending>,
[Symbol(async_id_symbol)]: 38,
[Symbol(trigger_async_id_symbol)]: 5
}
>
*/
rtrn(p,200)
// rename <resolve> to <rtrn> IS for api-compatible-with-nvlang
// <resolve> in nvlang IS used by dag-excutor'multi-cast-notify
/*
> p
Promise {
200,
[Symbol(async_id_symbol)]: 38,
[Symbol(trigger_async_id_symbol)]: 5
}
>
*/
var p = creat_one();
/*
> p
Promise {
<pending>,
[Symbol(async_id_symbol)]: 362,
[Symbol(trigger_async_id_symbol)]: 5
}
>
*/
thrw(p,404);
// rename <reject> to <thrw> IS for api-compatible-with-nvlang
// <reject> in nvlang IS used by dag-excutor'multi-cast-notify
/*
> Uncaught 404
>
> p
Promise {
<rejected> 404,
[Symbol(async_id_symbol)]: 362,
[Symbol(trigger_async_id_symbol)]: 5
}
>
*/
var ps = creat_many(10000000);
compare mem-usage with pure js layer defer methods
/*----------------------------------
var rss_si = process.memoryUsage().rss;
var ps = creat_many(10000000);
var rss_ei = process.memoryUsage().rss;
console.log("reflect style cost:",rss_ei-rss_si);
//stop exit to observe mem
var net = require('net');
var server = net.createServer(function(connection) {
});
server.listen(8080, function() { });
------------------------------------------------*/
TEST# node low-mem.js
reflect style cost: 685867008
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11394 root 20 0 1664076 714120 35932 S 0.0 4.4 0:06.88 node // 16G*0.044 = 700M
/*----------------------------------
var rss_si = process.memoryUsage().rss;
var ary_of_t3 = [];
for(let i=0;i<10000000;++i) {
let RS;
let RJ;
var P = new Promise(
(rs,rj) => {
RS = rs;
RJ = rj;
}
)
ary_of_t3.push(P,RS,RJ)
}
var rss_ei = process.memoryUsage().rss;
/*
%DebugPrint(ary_of_t3[0]);
%DebugPrint(ary_of_t3[1]);
%DebugPrint(ary_of_t3[2]);
*/
console.log("js style cost:",rss_ei-rss_si);
//stop exit to observe mem
var net = require('net');
var server = net.createServer(function(connection) {});
server.listen(8080, function() { });
console.log(ary_of_t3.length)
------------------------------------------------*/
/*
/TEST# node hig-mem.js
js style cost: 2880438272
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
11428 root 20 0 3778364 2.7g 38964 S 0.0 17.4 0:18.88 node // 16G*0.174 = 2850.816M
*/
creat_one( ): Promise
rtrn (p:Promise,v:Any): Void // first-param p MUST be Promise, IF not, crash
thrw (p:Promise,v:Any): Void // first-param p MUST be Promise, IF not, crash
creat_many(cnt:<uint32_t>): Array<Promise> // cnt MUST be uint32 AND cant be 0, IF not, crash