1

This is a 'why' question, not a suggestion. I know the following function is incorrect, but it helps to clarify my question: I want to know what is wrong with this view (i.e. what requirement of Promise I am missing here). Answers can help other like me to get out of confusion. Please provide an example where the following requirement of Promise is necessary:

I (incorrectly) assumed Promises are for deferring execution of functions and assigning a chain of then & catch. I need to know the following: Why does JavaScript Promise uses (resolve, reject) instead of a parameter-less callback that returns or throws?

In a naive and incorrect view, the Promise could have been used in a simpler way (without two arguments resolve, reject) like the following:

/*new*/ IncorrectPromise(()=>{
    let x = Math.random()>0.5;
    console.log('promise 1');
    if (x) {
        return 'head1';
    } else {
        throw new Error('tail1');
    }
})
.then((d)=>{
    console.log('.then() d=', d);
})
.catch( (ex) =>{
    console.log('.catch() e=', ex.message);
});

Instead of the Promise's way:

new Promise( (resolve_, reject_) => {
    let y = Math.random()>0.5;
    console.log('promise 2');
    if (y) {
        resolve_('head2');
    } else {
        reject_(new Error('tail2'));
    }
})
.then((d)=>{
    console.log('              .then() d=', d);
})
.catch( (ex) =>{
    console.log('              .catch() e=', ex.message);
});

i.e. when the control flow exits the body of the callback of the promise, resolve or reject are used based on whether the exit was caused by a return or a throw.

My question is, why promises use the current syntax? The answer will be an example which cannot be done with this (naive) function.

The above syntax can be easily emulated with the following definition (for simplicity, ignore the fact that IncorrectPromise is not a constructor, hence does not need the new operator):

function IncorrectPromise(callback) {
    return new Promise( (res, rej) => {
        try {
            res( callback() );
        } catch (err) {
            rej(err);
        }
    });
}

What would have been the shortcoming of this simpler usage syntax? What was the intention of the designers or the Promise to use (resolve, reject)?

Sohail Si
  • 2,750
  • 2
  • 22
  • 36
  • 1
    Because they are functions, passed as arguments to your callback. Not keywords. Their names are arbitrary, using `resolve` and `reject` is just a convention. – Bergi Mar 09 '19 at 22:57
  • As far as I understand, `Promise` is a class, not a function. It is used to create a new Promise object, so returning anything doesn't make sense. –  Mar 09 '19 at 22:58
  • 2
    The point is that they are callbacks which you are supposed to call asynchronously (if you don't, you wouldn't need a promise - your example snippet is very misleading). The executor callback cannot synchronously `return` or `throw` asynchronous results. – Bergi Mar 09 '19 at 22:59
  • @ChrisG I already wrote that in the question. My question is about something else. – Sohail Si Mar 09 '19 at 23:07
  • @Bergi I know, but the question is not about those parameter (argument) names. – Sohail Si Mar 09 '19 at 23:07
  • @Bergi Your answer to that question can be answer to my question too. But the answer for a totally different question. So this deserves being a separate question. Your answer was the following : `new Promise(_, reject) { setTimeout(reject, 1000); });` – Sohail Si Mar 09 '19 at 23:17
  • Based on that answer of yours, I would say the answer to my question is, a promise allows a chain of callbacks to be initiated in the promise body, where the innermost callbacks don't have the context. Hence, the `return` and `throw` will be useless, not be able to do the intended job. Because they don't have the context. Hence, a useful feature of promise is that `resolve` and `reject` arguments can be accessible inside the Promise's callback closure. This is not possible with my suggested syntax. – Sohail Si Mar 09 '19 at 23:25
  • But we cannot this in `.then()`, is that correct? Because `.then()` follows the syntax I mentioned. But `.then()` does not have a main capability that promise's body has. For example, the following case: `new Promise(...) .then((d)=>{ setTimeout( ()=>{ /* something that throws */ }, 1000); })`. In such cases, we need a new promise (a second promise) within the `.then()`'s body. – Sohail Si Mar 09 '19 at 23:35
  • 1
    @SohailSi Yes, that's correct. – Bergi Mar 10 '19 at 12:47

0 Answers0