Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

61
Visualizações
One Promise from multiple files

The example below shows a snippet of my application. The assumption is: some functions are executed after the document is loaded, others only after the POST response. On this momet, callbacks are placed in the array and executed after the request is completed. It all works as expected. I would like to change these callbacks to one Promise which would be fired just like now "Update.endRequest()"; I wonder if it is possible? Any suggestions are welcome. Or an idea if it can be done better.

update.js

class UpdateClass {
    constructor() {
        this.callbacks = [];
    }

    success(callback) {
        this.callbacks.push(callback);
    }

    endRequest() {
        this.callbacks.forEach(callback => callback());
    }
}

const Update = new UpdateClass();
export default Update;

file1.js

import Update from './update';

export default class File1Class {
    constructor () {
        this.init();
    }

    init() {
        Update.success(this.update);

        exampleFunction();
    }

    update() {
        exampleFunctionAfterUpdate();
    }
}

function exampleFunction() {
    console.log('On document load 1');
}

function exampleFunctionAfterUpdate() {
    console.log('After Update 1');
}

file2.js

import Update from './update';

export default class File2Class {
    constructor () {
        this.init();
    }

    init() {
        Update.success(this.update);

        anotherExampleFunction();
    }

    update() {
        anotherExampleFunctionAfterUpdate();
    }
}

function anotherExampleFunction() {
    console.log('On document load 2');
}

function anotherExampleFunctionAfterUpdate() {
    console.log('After Update 2');
}

app.js

import Update from './update';
import File1Class from './file1';
import File2Class from './file2';

class AppClass {
    constructor () {
        this.init();
    }
    
    init() {
        new File1Class();
        new File2Class();
        
        triggerfunction();
    }
}

function triggerfunction() {
    const button = document.getElementById('trigger');
    
    button.addEventListener('click', () => {
        xhr.open('POST', 'someUrl', true);
        xhr.onload = () => {
            if (xhr.status === 200) {
                Update.endRequest();
            }
        };
        xhr.send(someJsonData);
    });
}

window.app = new AppClass();
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use something like this

const promiseObj = new Promise((resolve,reject)=>{
  exampleFunctionAfterUpdate();
  anotherExampleFunctionAfterUpdate();
  resolve(true);
});

function triggerfunction() {
    const button = document.getElementById('trigger');
    
    button.addEventListener('click', () => {
        xhr.open('POST', 'someUrl', true);
        xhr.onload = () => {
            if (xhr.status === 200) {
                // Update.endRequest();
                promiseObj.then(()=> console.log('Functions executed'));
            }
        };
        xhr.send(someJsonData);
    });
}

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda