Nelson's log

Javascript Promises wot I learned

I program in 2006-era Javascript. But recently I started doing some more modern stuff. Arrow functions, those are great! And easy. Promises! Those are great. And confusing. Here’s some notes about what I’ve learned about Promises.

Warning: I’m a n00b here, some of this info could be wrong.

This code is laughably simple, but I found it useful when learning about Promises:


var state = 1;



p1 = new Promise((resolve, reject) => {
    console.log("p1 created");
    setTimeout(function() {
        console.log("p1 finished");
        state = 2;
    }, 1000);

console.log("made p1");
p1.then(function() { console.log(state); });
console.log("made p1.then");