0

Going over promises, I was wondering why, using the code bellow, the .then/catch calls are completed after the .then(onF, onR). Are the catch calls being placed at the bottom of the queue stack? I already understand the difference between the call stack and the message queue(queue stack) and the fact that the call stack is emptied before the message queue starts to be processed.

function capitalize(text) {
    return text[0].toUpperCase() + text.substr(1);
}

console.log(capitalize("hello"));

function capitalize2(text) {
    return new Promise(function (resolve, reject) {
        const result = text[0].toUpperCase() + text.substr(1);
        return resolve(result);
    });
}

capitalize2("hola").then((result) => console.log(result));
const pendingPromise = capitalize2("hola2").then((result) => result); // does  nothing

// Adding reject
function capitalize3(text) {
    return new Promise(function (resolve, reject) {
        if (typeof text !== "string") {
            return reject("Argument wrong type");
        }
        const result = text[0].toUpperCase() + text.substr(1);
        return resolve(result);
    });
}

capitalize3("bye").then((result) => console.log(result));
capitalize3(1111).then((value) => {console.log(value);}, (reason) => {console.log('1111 ' + reason);});
capitalize3(2222).then((result) => console.log(result)).catch((error) => console.error('2222 ' + error));

function capitalize4(text) {
    return new Promise(function (resolve, reject) {
        if (typeof text !== "string") {
            return reject(new Error("Argument wrong type"));
        }
        const result = text[0].toUpperCase() + text.substr(1);
        return resolve(result);
    });
}

capitalize4('hmmm').then((result) => console.log(result));
capitalize4(3333).then((value) => {console.log(value);}, (reason) => {console.error('3333 ' + reason);});
// with error object.
capitalize4(4444).then((result) => console.log(result)).catch((error) => console.error('4444 ' + error.message));
// witch catch
capitalize4(5555).then((value) => {console.log(value);}, (reason) => {console.error('5555 ' + reason);});

The output in node.js

Hello
Hola
Bye
1111 Argument wrong type
Hmmm
3333 Error: Argument wrong type
5555 Error: Argument wrong type
2222 Argument wrong type
4444 Argument wrong type

I placed the capitalize function calls in the order they're expected to be completed, even if they're async calls.

  • The [main difference between `.then(…, …)` and `.then(…).catch(…)`](https://stackoverflow.com/q/24662289/1048572) is not about the timing. The timing is also slightly different, but that's just because in the latter case the promise chain is longer - just the same reason why `Promise.resolve(1).then(console.log).then(() => console.log(3)); Promise.resolve(2).then(console.log)` has a timing where the two [independent promise chains](https://stackoverflow.com/q/36870467/1048572) are interspersed. – Bergi Apr 08 '23 at 22:49
  • Also: https://stackoverflow.com/q/59761840/1048572, https://stackoverflow.com/q/46408228/1048572 and more – Bergi Apr 08 '23 at 22:55
  • I trully recommend you replacing **then** and **catch** using **async/await**. You can get more information about that [in this answer](https://stackoverflow.com/a/73504237/9114389) – Vinicius Andrade Apr 08 '23 at 22:58
  • 1
    @ViniciusAndrade It's really hard to [replace `.then(…, …)` with an `await` version](https://stackoverflow.com/a/44664037/1048572). But either way, that does not appear to be the question here. – Bergi Apr 08 '23 at 23:03

1 Answers1

1

Promise.then(onFulfilled, onRejected) means Wait for Promise. If it fails call onRejected

Promise.then(onFulfilled).catch(errorFunc) means Wait for Promise and then for onFulfilled. If either fails call onRejected

Konrad
  • 21,590
  • 4
  • 28
  • 64
  • 2
    I wouldn't use the term "Wait". `onFulfilled` and `onRejected` and `errorFunc` are registered as listeners on specific promises. When the promise changes state, the appropriate ones will be called. The interpreter does not "wait" on this line of code. It continues to execute the subsequent lines of code. – jfriend00 Apr 08 '23 at 23:36