Skip to content

AsyncFunction

Namhyeon, Go edited this page Jun 5, 2022 · 11 revisions

AsyncFunction

JScript does not support the async keyword. So, WelsonJS uses the AsyncFunction class made by itself.

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;

In the console

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

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

function _async_sub_01(args) {
    var x = args[0];

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

function main(args) {
    if (!AsyncFunction.bind(exports, args)) {
        onShoutcut(args);
    }
}

exports.main = main;
exports.onShoutcut = onShoutcut;
exports._async_sub_01 = _async_sub_01;

You can use the _async_ prefix to create an asynchronous function.