scripting / Scripting-News

I'm starting to use GitHub for work on my blog. Why not? It's got good communication and collaboration tools. Why not hook it up to a blog?
121 stars 10 forks source link

Simpler server-side JavaScript #178

Open scripting opened 4 years ago

scripting commented 4 years ago

In a blog post I wrote:

I’d like to do a new server-side JavaScript environment that’s compatible with Node, ie runs Node apps, no breakage, but it also eliminates the need for callbacks, promises or async/await. I believe it’s technically possible.

A friend suggested using Babel to get started.

"I could imagine a modifier keyword on a function that means this function is internally going to do async things and calls to it should block until until it completes. That could be translated by a Babel plugin into somewhat that internally uses promises queuing to make callers wait for completion. "

This is the way to start. I wonder if anyone reading this has the expertise in Babel to make this work. I'll start a thread over in the repo to discuss.

This is that thread. :boom:

scripting commented 4 years ago

I will write up some code to demonstrate what the Babel plug-in will do, hopefully later today. Stay tuned.

scripting commented 4 years ago

Here are two examples, in one, folderloader.load is a JavaScript routine running in the current Node. It downloads a folder on S3 into a folder on the local disk, and when it's done, it calls a callback with some text for a log.

function loadFromConfigList (callback) {
    var logarray = new Array ();
    function loadone (ix) {
        if (ix >= config.locations.length) {
            callback (logarray);
            }
        else {
            var loc = config.locations [ix], logtext;
            folderloader.load (loc.s3path, loc.folder, function (logtext) {
                if (logtext.length == 0) {
                    logtext = "No changes.";
                    }
                logarray.push (logtext);
                loadone (ix + 1);
                });
            }
        }
    loadone (0);
    }

The second example, below, would be running in a new JavaScript engine, that hides the details of synchronization behind an interface.

In this version, folderloader.load doesn't return until it has completely downloaded the folder, returning the log text as the returned value of the function.

function loadFromConfigList () {
    var logarray = new Array ();
    config.locations.forEach (function (loc) {
        var logtext = folderloader.load (loc.s3path, loc.folder);
        if (logtext.length == 0) {
            logtext = "No changes.";
            }
        logarray.push (logtext);
        });
    return (logarray);
    }

In the second example folderloader.load would be specified with a new keyword instead of function.

var folderloader = {
   sync function load (s3path, localfolder) {
       //do the thing
       return ("the log text");
       }
   }