Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smaller build-up to promises? #14

Open
payne-chris-r opened this issue Dec 8, 2016 · 5 comments
Open

Smaller build-up to promises? #14

payne-chris-r opened this issue Dec 8, 2016 · 5 comments

Comments

@payne-chris-r
Copy link
Contributor

  1. Show a promise.
new Promise((resolve, reject) => { 
    if (false) {
      reject(error);
    }
   resolve(true);
  });
});
  1. 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);
  });
});
  1. 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);
  });
});
  1. 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);
    });
  });
};
  1. Keep going with .then()
@jrhorn424
Copy link

jrhorn424 commented Dec 8, 2016

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
Copy link
Contributor

raq929 commented Dec 13, 2016

I like this idea.

@payne-chris-r
Copy link
Contributor Author

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
Copy link

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
Copy link
Contributor

raq929 commented Feb 27, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants