ga-wdi-boston / node-api-promises

Other
0 stars 104 forks source link

Smaller build-up to promises? #14

Open payne-chris-r opened 7 years ago

payne-chris-r commented 7 years ago

1) Show a promise.

new Promise((resolve, reject) => { 
    if (false) {
      reject(error);
    }
   resolve(true);
  });
});

2) Show a promise where the callback makes a decision (and becomes the catalyst for the promise chain):

const flipCoin = function(){ /*50/50 chance of returning true*/ });

new Promise((resolve, reject) => { 
    if (flipCoin) {
      reject(error);
    }
   resolve(true);
  });
});

3) give flip a coin a setTimeout <-- so the call takes awhile

const flipCoin = function(){ /*waits 5 seconds, THEN has 50/50 chance of returning true*/ });

new Promise((resolve, reject) => { 
    if (flipCoin) {
      reject(error);
    }
   resolve(true);
  });
});

4) use fs.writeFile instead of setTimeout

const writeFile = (filename, content, options) => {
  return new Promise((resolve, reject) => { // save it
    fs.writeFile(filename, content, options, error => {
      if (error) {
        reject(error);
      }

      resolve(true);
    });
  });
};

5) Keep going with .then()

jrhorn424 commented 7 years ago
new Promise((resolve, reject) => { 
-    if (false) {
-      reject(error);
+    if (error) {
+      reject(false);
    }
   resolve(true);
  });
- });

Oh, I see now. I should have read more carefully. This diff misses your intent.

However it also muddies the common pattern we want them to use when writing their own Promise executors:

new Promise((resolve, reject) => { 
  if (error) {
    reject(error);
  }
  resolve(true);
});
raq929 commented 7 years ago

I like this idea.

payne-chris-r commented 7 years ago

Agreed, my intent is simply to show them the guts of a promise. THEN introduce what I've been calling the "catalyst function" that determines whether or not the original promise is resolved or rejected...

jrhorn424 commented 7 years ago

I think I do something like this in the delivery. @raq929, will you add our discussion of this issue to delivery notes and then ping @payne-chris-r? Many thanks.

raq929 commented 7 years ago

https://github.com/ga-wdi-boston/node-api-promises/wiki/Delivery-Notes