JDownloader Community - Appwork GmbH

Thread Tools Display Modes
Prev Previous Post   Next Post Next
Old 08.12.2019, 20:01
noone1's Avatar
noone1 noone1 is offline
JD Beta
Join Date: Oct 2013
Posts: 50
Default [javascript] callAsync and named callback / lost in asynchronous space...


I didn't find a way to use callback function in callAsync, so I ended up with something that seems overcomplicated. If somebody could give me an example to 're-sync' the standard output in the flow in this case.

Here I call a function that contains a callAsync and needs its result to go on, but in the context of this function I don't see variable accessible for a callback.

Meanwhile, here is my dirty, messy attempt:), while it is working I'm hopping something shorter:
I use an object.watch polyfill that switch a global param on variable change and use a while loop to check if the switch has been modified!

The problematic call from which I need a return inside the function's context is a simple test of echo %userprofile%.

I put functional script but it's part of a larger, so sorry for the mess;)

(At the beginning I just wanted to play a little with msdos hybrids to learn a bit of javascript but now I think I spent some time on SO and MDN and saw the evilness of javascript, lol.)


'use strict';


// non native object.watch polyfill
//Eli Grey initial version, improved Xose Lluis

if (!Object.prototype.watch) {

   "watch", {
     enumerable: false,
     configurable: true,
     writable: false,
     value: function (prop, handler) {
       var old = this[prop];
       var cur = old;
       var getter = function () {return cur;};
       var setter = function (val) {
        old = cur;
        cur = handler.call(this,prop,old,val);
        return cur;
       // can't watch constants
       if (delete this[prop]) {
            get: getter,
            set: setter,
            enumerable: true,
            configurable: true

// settings

const crlf="\r\n";
const sp=' ';
const bs=getEnvironment().pathSeparator;
const JD_TEMP=JD_HOME+bs+"tmp"+bs;

var start_date = new Date();
var today = start_date.toLocaleString();
var start_msg='-- start@'+today+crlf+crlf;

//obj to watch + switch
var getsw=0;
var geto = { mypath: '', username: '', usertemp: ''};

//act as var listener (trigger when modified)
geto.watch('mypath', function (id, oldval, newval) { 
    return newval;}

//----------- user defined ----------------- 

var mypath;                    //specify a path for tmp files or default will be used (desktop here)

if (typeof(mypath)=="undefined") {        //using it will define username and user temp folder for logfile
    getLocalPath();}            //will wait for async var to be set
var log_file=mypath+'jsjd_log_tmp.txt';        // log file

var check_locpath='back to main:'+crlf+
'mypath:' +mypath+crlf+

logf(start_msg+"working path: "+mypath+crlf,true);

alert('Hello '+geto.username+" \u0110");

//-- end -- //

//set mypath

function getLocalPath(){ 
//function(e,s,r) in callAsync is the callback function except it is an annonymous (unamed) function
//need to send standard output to a parser and then have the result sync here
//I didn't find how to use it so I make it call another function

    callAsync(function(e,s,r){coutput(e,s,r,"path");},"cmd","/c",'@echo off & echo.%userprofile%"');

    sync(); //wait till getsw switch is set

    //now we have the result (geto) from the console log parser  and we can go on
    /*choose default*/
    var default_path=geto.mypath+bs+"Desktop"+bs;
    //var default_path=default_path+"JDscripts"+bs;

    if (getPath(default_path)) {
    } else {


function sync(dummy){//if watch switch used

    var i=0;
    while (this['getsw']!=1) {//global this
        if(i>3*Math.pow(10,6)) {break;}

//console log parser

function coutput(e,s,r,typ){

    //split console log 
    var b=s.split(crlf);

    /*output for local path*/
    if (typ=="path"){

        // mypath=b[0]=%userprofile%


function file_exist(myfile) {
    return getPath(myfile).exists();

function del_file(myfile){ //non recursive
    if (file_exist(myfile)) deleteFile(myfile,false);

function logf(log_content,bAppend){
//call a 1st time with true to create the file, then bAppend is obsolete

	if (typeof bAppend === 'undefined') bAppend = true;
	if (!bAppend) {
	} else {
		if (typeof log_file!=="undefined") {
			alert(' /!\\ attempt to log to undefined file /!\\'

function print_r(arr, level) {

    var dumped_text = "";
    if (!level) level = 0;

    var level_padding = "";//The padding given at the beginning of the line.
    for (var j = 0; j < level + 1; j++) level_padding += "    ";

    if (typeof(arr) == 'object') { 
        for (var item in arr) {
            var value = arr[item];

            if (typeof(value) == 'object') { 
                dumped_text += level_padding + item + " ...\n";
                dumped_text += print_r(value, level + 1);
            } else {
                dumped_text += level_padding + item + " => " + value + "\n";
    } else { 
        dumped_text = "=> " + arr + " <=(" + typeof(arr) + ")";
    return dumped_text;

>'msdos/js chimera' : we will rule the world!
>'py god' : calm down... you are era too early
>'vba Bellérophon': one day I will kill you both, I swear
>'wscript pegasus': me too, me too
>'php observer': haha, those kids are funny
>'brainf*ck alien': +[>.+<] bzzzt
>'whitepace void':
>'java nut': I think, therefore I am, I know I'm not crazy! anybody in there??

Last edited by noone1; 08.12.2019 at 22:43.
Reply With Quote

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT +2. The time now is 17:46.
Provided By AppWork GmbH | Privacy | Imprint
Parts of the Design are used from Kirsch designed by Andrew & Austin
Powered by vBulletin® Version 3.8.10 Beta 1
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.