异步JavaScript问题 [英] Asynchronous javascript issue

查看:116
本文介绍了异步JavaScript问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做一个函数详细信息()中查找特定事件的详细信息( getdetails )的基础上可用的事件。在下面的例子中,细节被要求用于与ID的ZRGZZ,RGHER和GRFDZ的事件。这些细节应该放在里面一个阵列(学分西奥多的答案)。结果应该是,当我称之为详细信息(),结果数组存储,所以我以后可以使用它。

问题是,我不知道我怎么能回到这最后的数组。 的console.log(JSON.stringify(RES)); 是完成阵列的建造之前执行。我知道这可能牵扯到异步JS,但我不能换我的头周围...

 功能的详细说明()
{
    VAR解析度= {
    结果:成功,
    键:12345,
    数据:[{
        ID:ZRGZZ
        lastChangedDate:2015年12月3日11时14分27秒
    },{
        ID:RGHER
        lastChangedDate:2015年12月3日15时17分47秒
    },{
        ID:GRFDZ
        lastChangedDate:2015年12月3日5点25分11秒
    }]
};    变种I = 0;
    VAR tmp目录;
    res.Data.map(功能(VAL,I){        getdetails(val.ID)。然后(功能(数据){
            TMP = JSON.parse(数据);
            的console.log(TMP);
            Object.keys(tmp.Data [0])。图(函数(五,J){
                VAL [V] = tmp.Data [0] [v]的;
                的console.log(JSON.stringify(RES)); //(*)的最后一个资源给了我,我要找的结果
            });        },功能(错误){//错误回调
            的console.log(错误)
        });
    });
的console.log(JSON.stringify(RES)); //这是之前执行(*)
}


解决方案

那么你已经在使用的承诺 - 看然后在code,如果它的角,然后然后自动返回延期的承诺,所以你可以这样做:

  res.Data.map(功能(VAL,I){
    getdetails(val.ID)。然后(功能(数据){
        TMP = JSON.parse(数据);
        Object.keys(tmp.Data [0])。图(函数(五,J){
            VAL [V] = tmp.Data [0] [v]的;
        });
    },功能(错误){//错误回调
        的console.log(错误)
    })。然后(函数(){
        的console.log(JSON.stringify(RES));
    })
});

编辑:注入服务 $ Q 到您的控制器或服务

 承诺= [];
res.Data.map(功能(VAL){
    promises.push(getdetails(val.ID)。然后(功能(数据){
        TMP = JSON.parse(数据);
        Object.keys(tmp.Data [0])。图(函数(五,J){
            VAL [V] = tmp.Data [0] [v]的;
        });
    },功能(错误){//错误回调
        的console.log(错误)
    }));
});
$ q.all(承诺)。然后(函数(){
    的console.log(JSON.stringify(RES));
});

现在,当所有的 getdetails 解析可以CONSOLE.LOG或做任何你想要的数据。

I'm trying to make a function details() that looks up the details of certain events (getdetails) based on the available events. In the example below, details are asked for the events with IDs ZRGZZ, RGHER and GRFDZ. These details should be placed inside one array (credits to Theodore's answer). The result should be that when I call details(), the resulting array is stored so I can use it later on.

The problem is that I don't know how I can return this final array. console.log(JSON.stringify(res)); is executed before the construction of the array is completed. I know this probably has something to do with asynchronous js, but I just can't wrap my head around it...

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 (*)
}

解决方案

Well you already use promise - see then in your code and if it's angular then then itself return deferred promise so you can do:

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: inject service $q into your controller or service

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));
});

now when all the getdetails are resolved you can console.log or do whatever you want with the data

这篇关于异步JavaScript问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆