ym.Async

Methoden

map(list, iterator, completeopt, asyncFnopt, contextopt) ab v3

Die Map-Funktion ist eine Wrapper-Komponente, die das Verhalten eines asynchronen Mappings steuert. Sie können Ihre eigene asynchrone Steuerungsfunktion erstellen oder mapSeries und mapParallel die Aufgabe erledigen lassen.

Parameter

Name Beschreibung Datentyp Standardwert
list   array  
iterator   function  
completeopt   function or null null
asyncFnopt   function ym.async.parallel
contextopt   object or null null

Beispielaufruf

Codebeispiel: map(list, iterator, completeopt, asyncFnopt, contextopt) ab v3

// the iterator is a function with the current item of the array and a
// callback. At the end of an asynchronous call the done() callback must
// be triggered to end the process.
function iterator (item, done) {
    // this is a simple echo script. It will answer in a time period
    // between 0 and 500 milliseconds.
    setTimeout(function () {
        console.log(item);
        done(null, item);
    }, Math.random() * 500);
}

// The complete callback will be triggered after any array element is executed.
function complete (err, results) {
    console.log("done!", err, results);
}

// your customized asynchronous iteration control.
function asyncFn (queue, complete, context) {
    var fn;
    var results = [];
    var len = queue.length;

    // the callback will be executed in the iterator after the asynchronous command is finished.
    // It is decrementing the length of the queue until every asynchronous command is complete.
    function next(err, data) {
        results.push(data);
        if (!--len || err) {
            complete.call(context || complete, err, results);
        }
    }

    // the main loop is done very quickly. It is a parallel loop.
    while (fn = queue.pop()) {
        fn.call(context || {}, next);
    }

    // an empty array causes the execution of the complete callback
    // instantly
    if (!len) {
        complete.call(context || complete, null, results);
    }
}

// the function call for the map function
ym.async.map(["a", "b", "c"], iterator, complete, asyncFn);

mapParallel(list, iterator, completeopt, contextopt) ab v3

Führt einen Iterator auf einem beliebigen Array-Element parallel aus. Der Callback enthält das resultierende Array.

Parameter

Name Beschreibung Datentyp Standardwert
list   array  
iterator   function  
completeopt   function or null null
contextopt   object or null null

Beispielaufruf

Codebeispiel: mapParallel(list, iterator, completeopt, contextopt) ab v3

// the iterator is a function with the current item of the array and a
// callback. At the end of an asynchronous call the done() callback must
// be triggered to end the process.
function iterator (item, done) {
    // this is a simple echo script. It will answer in a time period
    // between 0 and 500 milliseconds.
    setTimeout(function () {
        console.log(item);
        done(null, item);
    }, Math.random() * 500);
}

// The complete callback will be triggered after any array element is executed.
function complete (err, results) {
    console.log("done!", err, results);
}

// the function call for the map function
ym.async.mapParallel(["a", "b", "c"], iterator, complete);

mapSeries(list, iterator, completeopt, contextopt) ab v3

Führt einen Iterator auf einem beliebigen Array-Element nacheinander aus. Der Callback enthält das resultierende Array.

Parameter

Name Beschreibung Datentyp Standardwert
list array
iterator function
completeopt function or null null
contextopt object or null null

Beispielaufruf

Codebeispiel: mapSeries(list, iterator, completeopt, contextopt) ab v3

// the iterator is a function with the current item of the array and a
// callback. At the end of an asynchronous call the done() callback must
// be triggered to end the process.
function iterator (item, done) {
    // this is a simple echo script. It will answer in a time period
    // between 0 and 500 milliseconds.
    setTimeout(function () {
        console.log(item);
        done(null, item);
    }, Math.random() * 500);
}

// The complete callback will be triggered after any array element is executed.
function complete (err, results) {
    console.log("done!", err, results);
}

// the function call for the map function
ym.async.mapSeries(["a", "b", "c"], iterator, complete);

parallel(queue, completeopt, contextopt) ab v3

Führt asynchrone Methoden parallel aus und löst nach Beendigung einen Callback aus.

Parameter

Name Beschreibung Datentyp Standardwert
queue Die Warteschlange der Funktionen. Array.<function()>
completeopt Der Callback nach Beendigung. function or null null
contextopt Der Kontext für this. function or null null

series(queue, completeopt, contextopt) ab v3

Führt asynchrone Methoden nacheinander aus und löst nach Beendigung einen Callback aus.

Parameter

Name Beschreibung Datentyp Standardwert
queue Die Warteschlange der Funktionen. Array.<function()>
completeopt Der Callback nach Beendigung. function or null null
contextopt Der Kontext für this. function or null null