Using external variables with Javascript then
<p>I am new to javascript, have mostly spent my time writing backend java program. I am unable to figure out how to capture the current state of the scope variables when calling then, example:-</p>

<pre><code>idList = [0, 1, 2, 3, 4, 5];

for(id in idList) {
    new Promise(function(resolve, reject) { 
        // A mock async action using setTimeout
        setTimeout(function() {
            resolve(10); 
        }, 300);
    }).then(function(num) { 
        console.log(id + ': ' + num); 
        return num * 2; 
    });
}
</code></pre>

<p>The output is:-</p>

<pre><code>"5: 10"
"5: 10"
"5: 10"
"5: 10"
"5: 10"
"5: 10"
</code></pre>

<p>But I want the output to be </p>

<pre><code>"0: 10"
"1: 10"
"2: 10"
"3: 10"
"4: 10"
"5: 10"
</code></pre>

<p>i.e want then to capture the current state of variable id.</p>