JavaScript Asynchronous Error

I am trying to make a function details()that looks at the details of specific events ( getdetails) based on the available events. The following example retrieves event information with identifiers ZRGZZ, RGHER, and GRFDZ. This data should be placed within one array ( Theodore answer credits ). The result should be that when I call details(), the resulting array is saved, so I can use it later.

The problem is that I do not know how I can return this last array. console.log(JSON.stringify(res));runs until array completion is complete. I know this probably has something to do with asynchronous js, but I just can't wrap myself around ...

function details()
{
    var res = {
    "Result": "success",
    "Key": "12345",
    "Data": [{
        "ID": "ZRGZZ",
        "lastChangedDate": "2015-12-03 11:14:27"
    }, {
        "ID": "RGHER",
        "lastChangedDate": "2015-12-03 15:17:47"
    }, {
        "ID": "GRFDZ",
        "lastChangedDate": "2015-12-03 05:25:11"
    }]
};

    var i = 0;
    var tmp;
    res.Data.map(function(val,i){

        getdetails(val.ID).then(function(data){
            tmp = JSON.parse(data);
            console.log(tmp);
            Object.keys(tmp.Data[0]).map(function(v,j){
                val[v] = tmp.Data[0][v];
                console.log(JSON.stringify(res)); //(*)the last res gives me the result I'm looking for
            });

        }, function(error){ //error callback
            console.log(error)
        });


    });
console.log(JSON.stringify(res)); //this is executed before (*)
}
+4
2

, - . then , angular, then , :

res.Data.map(function (val, i) {
    getdetails(val.ID).then(function (data) {
        tmp = JSON.parse(data);
        Object.keys(tmp.Data[0]).map(function (v, j) {
            val[v] = tmp.Data[0][v];
        });
    }, function (error) { //error callback
        console.log(error)
    }).then(function () {
        console.log(JSON.stringify(res));
    })
});

EDIT: $q

promises = [];
res.Data.map(function (val) {
    promises.push(getdetails(val.ID).then(function (data) {
        tmp = JSON.parse(data);
        Object.keys(tmp.Data[0]).map(function (v, j) {
            val[v] = tmp.Data[0][v];
        });
    }, function (error) { //error callback
        console.log(error)
    }));
});
$q.all(promises).then(function () {
    console.log(JSON.stringify(res));
});

, getdetails , console.log , ,

0

- async, async.each async.eachSeries. ( async.map, , .)

, :

async.each(res.Data, function(val,callback){
    getdetails(val.ID).then(function(data){
        tmp = JSON.parse(data);
        Object.keys(tmp.Data[0]).map(function(v,j){
            val[v] = tmp.Data[0][v];
        });
        callback(null); // Asynchronous code has finished without error
    }, function(error){ //error callback
        callback(error); // Asynchronous code has finished with error
    });
}, function(error) {
    // All asynchronous calls have finished
    if(error)
        console.log("Error", error);
    else
        console.log("Success", res);
});

async.each , async.eachSeries .

+1

Source: https://habr.com/ru/post/1622059/


All Articles