Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

57
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!