I am a novice in javascript and I've been learning about promises, recently I started working in some simple list that is retrieved with a random delay (setTimeout), a classical I think. After trying and doing some research I found I post that helped me a lot, and it works fine, but after reviewing what that post suggest against I had done before I noticed that the difference are two brackets...I've been reading and trying to understand why that brackets make such a difference (both options are functional) but I don't get it.
Could some one explain what's the difference? The brackets I mentioned are in function loadIng(), lines 5 and 12, the open bracket in first line and the closing one in the second.
{
new Promise(resolve => {
...
})
}
If I run the code with the brackets it runs synchronously, but if I run it without the brackets it runs asynchronously...I really don't get it.
function loadIngs() {
p = Promise.resolve();
for (let [k, v] of someArray) {
p = p.then(() =>
{new Promise(resolve => { //this
delay = Math.random() * 2000;
console.log(`${k}:${v} comes delayed for ${delay}`)
setTimeout(() => {
console.log("executes some function")
resolve();
}, delay);
})} //this
);
}
}