2

Lets say I have a Promise like the following:

new Promise((resolve, reject) => {

  // Some random conditional
  if (today === 'Friday') {
    console.log("I'm resolving this thing here");
    resolve('taco');
    // Should I have a return true or false here?
  }

  console.log("Code made it past the first resolve");

  // Do some other code here, like connect to a database or API or something.
  connectToDatabase(function(result){
    // result was 'hamburger'
    resolve(result);
  });

}).then((value) => {
  console.log(value);
});

In this example, I get the following output:

I'm resolving this thing here
Code made it past the first resolve
taco

So, after I do a resolve, the Promise code continues to execute. In my above example, I unnecessarily connect to some database or endpoint or something. Obviously the send resolve is ignored, which is just fine. But connecting to databases and APIs is expensive from a service cost standpoint, especially if it costs you money every time you make a request like an AWS service or something.

So my question is, should I be putting in a return true; or something in after that first resolve?

Similarly with reject, should I put in a return false or (true??) after a reject?

new Promise((resolve, reject) => {
  someAsyncFunction((err, result) => {
    if (err) {
      reject(err);
      // Should I have a return true or false here or something?
    }

    // Do something here, like connect to a database or fetch a value from some API endpoint or something.

    resolve(result);
  });
});

If I don't want the code to be executed after that reject I should return, correct?

When I look through all sorts of ES6 documentation, I can't find any places where they make this clear... Lots of Promise examples are usually really simplified.

If I do return something on these, what should I return? true? false? or return resolve('taco');?

Jake Wilson
  • 88,616
  • 93
  • 252
  • 370
  • If you need to do another promise handle it in .then for resolve or .catch for reject. – Edgesoft Oct 20 '16 at 07:12
  • 1
    `Should I be putting in a return true; or something in after that first resolve?` not necessarily - it could be perfectly valid to continue with some other code after you resolve (or reject) - as long as you understand that any further resolves/rejects are ignored, there's no reason to "return" at that point – Jaromanda X Oct 20 '16 at 07:26
  • return Promise.reject(new Error(err)); its good to return reject – Umakant Mane Oct 20 '16 at 10:24
  • @UmakantMane. You *must* just use `reject` to settle the `Promise`. Your suggestion would actually leave the `Promise` to be unsettled potentially upholding the entire promise chain. – rabbitco Oct 20 '16 at 10:31
  • A Promise executor a.k.a `(resolve, reject) => ... ` function is a point of "point of no return" once it's invoked. Everything happening afterwards shall remain within executor's context. In your question you can use as many `resolve`s or `reject`s as you want but your algo should only invoke them either once or never. – Redu Oct 20 '16 at 11:06
  • @zeroflagL I'm not trying to resolve more than once. I'm trying to `resolve` a different value depending on certain conditions. For example, if condition === true, resolve something, otherwise do some stuff (like fetch a value from an API or something) and then resolve a different value. Was I not clear on that? – Jake Wilson Oct 20 '16 at 14:25
  • @JaromandaX But what if the code after the `reject` or `resolve` is something I don't want to execute? What if it involves connecting to API endpoints or fetching things from a database. Those kind of things are expensive from a processing standpoint and from a service cost standpoint. In that case, I should return after the `resolve` or `reject` right? I must have not been clear in my question since people really aren't understanding me it seems. – Jake Wilson Oct 20 '16 at 14:32
  • Also have a look at [Return value of Promise's resolve/reject functions](http://stackoverflow.com/q/31651610/1048572) – Bergi Oct 20 '16 at 14:32
  • "*If I don't want the code to be executed after that reject I should `return`*" - YES. Or you just use an `else` with your `if`. "*should I put in a return false or true?*" - the value is ignored anyway, so don't return anything and just put a `return;`. Or put the keyword before the reject: `return reject(…)`. – Bergi Oct 20 '16 at 14:43
  • 1
    Regarding the "*Do something here*" in your second snippet - you shouldn't be doing that. Just `resolve(result)` and don't do anything else. You are promisifying `someAsyncFunction`, everything what happens after that should go in a `then` callback. If you need to "*connect to a database or fetch a value from some API endpoint or something*", create an extra promise for that. – Bergi Oct 20 '16 at 14:45
  • Regarding your first example, see also [Idiomatically handling pre-conditions with ES6 Promises](http://stackoverflow.com/q/35856041/1048572) – Bergi Oct 20 '16 at 14:46
  • @Bergi I understand, but what if only sometimes I need to run `someAsyncFunction`? For example, if `today==='Friday'`, then `resolve(a)` otherwise, run `someAsyncFunction` and resolve a different value. Why should I split this up into multiple Promises if that extra bit of code is only executed sometimes? Also thank you for your responses. Very helpful. Feel free to user a proper answer below for some credit. – Jake Wilson Oct 20 '16 at 16:20
  • I just think that `if (today == 'Friday') return Promise.resolve(a); /*else*/ return new Promise(…)` is easier to read, and you get the necessary early return for free. – Bergi Oct 20 '16 at 16:29
  • Or was this directed at the comment "*regarding the Do something here*"? Then, the argument is basically that the `someAsyncFunction` callback is unsafe (errors thrown won't be caught) and you should promisify on the lowest level possible. Combining promises is more simple and safe (less errorprone) than combining multiple callbacks. – Bergi Oct 20 '16 at 16:31

1 Answers1

-3

basic level understanding of how promises works and when to use resolve, reject

   function d() {
     console.log("D started");
     return Promise.reject(new Error("Aborted from D function"));

   //return Promise.resolve("Done with D");
 }

function c() {
console.log("C Started");
//return Promise.reject(new Error("Aborted from C function"));
//return Promise.resolve("Done with C");
return d().then(result => {
       return Promise.resolve(result);  
}).catch(err => {
       return Promise.reject(new Error(err));
});
}

function b() {
  console.log("B Started");
  //return Promise.reject(new Error("Aborted from B function"));
 return Promise.resolve("Done With B");
   }

    function a() {
    console.log("A started");
    return b().then(function(resultb){
        console.log("B result:"+resultb);
        return c();
 }).catch(function(err){
        console.log("Error:"+err)
}).then(function(resultc){
       console.log("C result:"+resultc)
}).catch(function(err){
    console.log("Error:"+err);  
  });
   }

  a();
Umakant Mane
  • 1,001
  • 1
  • 8
  • 9