How to understand the data flow of a Promise in ES6?

0

I was studying the working of Promise in ES6 and came across a pseudo-code of implementing a simple Promise:

class PromiseSimple {

    constructor(executionFunction) {
        this.promiseChain = [];
        this.onResolve    = this.onResolve.bind(this);
        this.onReject     = this.onReject.bind(this);

        console.log(this.onResolve); // [Function: bound onResolve]

        // Nesse ponto o que this.onResolve está armazenando?
        // O que significa [Function: bound onResolve] e qual a
        // relação do bind nisso tudo?

        executionFunction(this.OnResolve, this.onReject);
    }

    onResolve(value) {
        let storedValue = value;
        // o que está vindo no parâmetro value, seria 'Fine'?

        this.promiseChain.forEach((nextFunction) => {
            storedValue = nextFunction(storedValue);
        });
        // Quando o método retona storedValue?
    }

    then(onResolve) {
        this.promiseChain.push(onResolve);
        // o parâmetro onResolve é a função console.log()?

        return this;
        // Para onde estou enviando esse this?
    }
}

const myPromise = new PromiseSimple((onResolve, onReject) => {
    setTimeout(onResolve, 3000, 'Fine');
    // Estou enviando 'Fine' ao método onResolve da Promise?
});

myPromise.then((response) => {
    console.log(response); // Fine
});

Note: My doubts are in the comments of the code. I removed error handling from the code in order to make it cleaner.

To access the full code, follow the link: link

    
asked by anonymous 21.05.2018 / 19:33

0 answers