0

I'm trying to create a JavaScript object (friends_ringsigs), in which the keys are the values of names[i], and the values are the "result" variable created at the execution of the promise. The problem is, when I try to access the "names[i]" variable for use, it doesn't exist.

// get list of new contact requests
$.ajax({
    url: '/api/messaging/getnewfriends.php?auth=<?=$_SESSION['auth_random']?>',
    type: 'POST',
    data: '',
    success: function(result) {
        if(result != 'none'){
            var names = result.split("[Delimiator0]");
            for(var i = 0; i < names.length-1; i++){
                ringsig_decrypt(priv_ck, names[i]).then(function(result){
                    friends_ringsigs[result] = names[i];
                    alert(friends_ringsigs[result]);
                    alert(names[i]);
                    alert(result);
                    document.getElementById('newcontactslist').innerHTML += contactify(result);
                    $('#contactslabel').show();
                });
            }
        }else{
            document.getElementById('newcontactslist').innerHTML = "";
            $('#contactslabel').hide();
        }
    }
});

I'm able to access the "result", but not the names[i], and later when I go to get the value out of friends_ringsigs, it doesn't exist. In fact, alert(JSON.stringify(friends_ringsigs)); outputs "{}".

halfer
  • 19,824
  • 17
  • 99
  • 186
Ty Everett
  • 59
  • 9
  • 1
    Since promise is called asynchronously, the for loop is completed before hand and value of i will be `names.length`. You will have to use closure – Rajesh Dec 04 '17 at 04:52
  • I'd recommend `names.forEach(name => { ... })` – Phil Dec 04 '17 at 05:05
  • Added the answer in the question since while writing the answer the question was closed. – HMR Dec 04 '17 at 05:09
  • @HMR that's not how this works. If the question is closed as a duplicate, the answer can be found in the linked post – Phil Dec 04 '17 at 22:52
  • Hello Ty, there is a link under the question saying `edited .... ago`. If you click that link you can find the answer. Since this is a duplicate and refers to questions with 37 answers of which none use the cleanest option map; I thought i'd be best to identify where you made a mistake and how to solve it best (Phil's comment on array.forEach was a good hint although I would prefer map). – HMR Dec 05 '17 at 03:26
  • @Phil OP already has an array that needs to be mapped to promises so cleanest solution is not in the 37 answers totaling the linked "duplicates". I could add the most appropriate answer to one of those questions and link it here in the comments but both questions use loops, (imperative style )not arrays (declarative) so that answer would probably not be appropriate to the question. – HMR Dec 05 '17 at 03:31
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Dec 07 '17 at 15:00
  • @HMR: if you want to add another answer on a closed question, a good way to do it is to prepare a Gist or pasteboard, and add the link into the comments. – halfer Dec 07 '17 at 15:01
  • @halfer Yes, that's a good idea, will try that next time. – HMR Dec 07 '17 at 15:24

0 Answers0