0

I will try to explain my problem in most understandable way.

  1. Description
  2. Problem

1. Description

tl;dr console.logging displays array data which is being assigned to it after performing ajax request in the future. Displaying it before assignment!

this is my js structure:

var details = {
    var contactsData = [];
    [...]

    saveNotes: function() {
     console.log(this.contactsData);
     this.saveDefault();
    }

    saveDefault: function() {
        var that = this;
        $.ajax({
            [...]
        }).done(function (response) {
            that.contactsData[that.contactsData.length] = response;
            console.log(that.contactsData);
    });
}

STEP 1: I have an input field. Using event querySelector and keypress eventListener I call saveNotes()

STEP 2: saveNotes() console logs this.contactsData before calling another function.

STEP 3: I execute saveDefault() and call ajax, it returns response which I attach to contactsData as a new value and console.log this.contactsData.

2. Problem

Console.log in SaveNotes() logs this Consolelog1

and console log in .done logs this

consolelog2

Note the (26) vs (27) info in inspector and length: value

How this is even possible, help please and thanks.

Kacper Cieluch
  • 437
  • 3
  • 16
  • I don't see how you can tell the output of the `console.log()` calls apart. It's a good idea to log some sort of label so that it's unambiguous. – Pointy Mar 01 '18 at 14:45
  • 2
    Possible duplicate of https://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays – Jeto Mar 01 '18 at 14:47

0 Answers0