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

102
Views
Promise based counter

I am new to learning JavaScript and so have begun understanding promises. I have written a simple code that logs the value passed to promise function as parameter, after setTimeout goes off. If i need to create a counter using the same code, is there a way I can do that?

function hello(a, b) {
    let promise = new Promise((res, rej) => {
        setTimeout(() => {
            res(a);
        }, b);
    }).then((result) => {
        console.log(`Result: ${result}`);
    });
}
hello(5, 1500);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you want to get the numbers of times hello's promise resolved.

Simply add a count variable and increment each time promise resolved

let count = 0;
function hello(a, b) {
    let promise = new Promise((res, rej) => {
        setTimeout(() => {
            res(a);
        }, b);
    }).then((result) => {
        count++;
        console.log(`Result: ${result}, Count: ${count}`);
    });
}
hello(5, 1500);
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!