7 Functions
Namhyeon, Go edited this page 2024-08-14 14:02:06 +09:00

Functions

JScript does not support the async and Generator keywords. So, WelsonJS uses the AsyncFunction, GeneratorFunction class made by itself.

AsyncFunction

Examples

In the webbrowser

var STD = require("lib/std");

function main(args) {
    [5, 4, 3, 2, 1].forEach(function(x) {
        new AsyncFunction(function() {
            sleep(x * 1000);
            console.log(x);
        }).run();
    });
}

exports.main = main;

// You must explicitly specify where to find the function.
AsyncFunction.__filename__ = "program.js";

In the console

var STD = require("lib/std");

function onShoutcut(args) {
    [5, 4, 3, 2, 1].forEach(function(x) {
        sub_01.run();
    });
}

var sub_01 = new AsyncFunction(function(args) {
    var x = args[0];

    sleep(x * 1000);
    console.log(x);
});

function main(args) {
    // If this is completed, stop with an AsyncFunction.Resolved exception.
    AsyncFunction.Initialize(exports, args);

    // If it does not try to call an async function.
    onShoutcut(args);
}

exports.main = main;
exports.onShoutcut = onShoutcut;
exports.sub_01 = sub_01;

// You must explicitly specify where to find the function.
AsyncFunction.__filename__ = "program.js";

GeneratorFunction

Examples

var a = new GeneratorFunction(function(_yield) {
    _yield("a");
    _yield("b");
    _yield("c");
});
console.log(a.next().value);
console.log(a.next().value);
console.log(a.next().value);

Contact me