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

192
Views
How to catch a rejected promises in same time order as resolves?

Promises which resolve() or reject() can be handled in order with .then() regardless if they are resolved or rejected:

const getRecordById = (id) => {
    return new Promise((resolve, reject) => {
        const randNum = Math.floor(Math.random() * 2) + 1;
        if (randNum === 1) {
            resolve({ id, firstName: 'John', lastName: 'Doe' });
        } else {
            reject(`user id ${id} not found`);
        }
    });
};

for (let id = 1; id <= 10; id++) {
    getRecordById(id)
        .then(
            (record) => { console.log(`you got record #${record.id}`); },
            (errMessage) => { console.log(`error was: ${errMessage}`); }
        );
}

enter image description here

But I read in articles like this one that e.g.

many libraries and frameworks assume that promises are always rejected with an error

and so one should instead reject with an error like this: reject(new Error('Oops!')); instead of rejecting with a string.

But when I reject with an Error object, why do all the rejections complete after the resolves?

const getRecordById = (id) => {
    return new Promise((resolve, reject) => {
        const randNum = Math.floor(Math.random() * 2) + 1;
        if (randNum === 1) {
            resolve({ id, firstName: 'John', lastName: 'Doe' }); 
        } else {
            reject (new Error(`user id ${id} not found`)); 
        }
    });
};

for (let id = 1; id <= 10; id++) {
    getRecordById(id)
        .then((record) => { console.log(`you got record #${record.id}`); })
        .catch((errMessage) => { console.log(`error was: ${errMessage}`); });
}

enter image description here

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In the second example the errors are shown at the bottom is not because reject was an error object. In the first code you have handled error inside .then but in the second code snippet there is .catch for handling exception. Second code will log in same order as first if you handle error inside .then.

const getRecordById = (id) => {
    return new Promise((resolve, reject) => {
        const randNum = Math.floor(Math.random() * 2) + 1;
        if (randNum === 1) {
            resolve({ id, firstName: 'John', lastName: 'Doe' }); 
        } else {
            reject (new Error(`user id ${id} not found`)); 
        }
    });
};

for (let id = 1; id <= 10; id++) {
    getRecordById(id)
        .then((record) => { console.log(`you got record #${record.id}`)},
            (errMessage) => { console.log(`error was: ${errMessage}`); })
}

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!