Giter VIP home page Giter VIP logo

node-promise-example's Introduction

node-promise-example

A basic example of getting input from the user and using a promise to continue program execution after the line of input is received.

$ node example.js
Enter some text and press enter: I am a great beer!
You entered: I am a great beer!

Take a look at the code:

var Promise = require('promise');

var theyPressedEnter = function(input) {
  return input.indexOf('\n') != -1;
};

var getInput = function() {
  return new Promise(function(resolve, reject) {
    var input = '';

    process.stdin.resume();
    process.stdin.setEncoding('utf8');

    process.stdin.on('data', function(chunk) {
      input += chunk;

      if (theyPressedEnter(chunk)) {
        input = input.replace('\n', '');
        process.stdin.pause();
        resolve(input);
      }
    });
  });
};

process.stdout.write("Enter some text and press enter: ");

getInput().then(function(theInput) {
  console.log("You entered: " + theInput);
});

Note that process.stdin has a end event however it is triggered with the end of stream or CTRL-D sequence. We want only a single line of input. So we check the chunk that is provided by the data event to see if it contains \n and if it does, we resolve the promise with the full input. The program execution then continues with the then function passing the input to it.

reject

Notice in the above code we one of the parameters at the top of the function is reject yet it is not used. Well this allows us to reject the promise. Say something bad happened like we couldn't read from process.stdin. In that case, we could handle the error by rejecting the promise with an error message. The execution would then go to the second function in the then call. A really basic example of rejecting:

var Promise = require('promise');

new Promise(function(resolve, reject) {
    reject('something really bad happened');
}).then(
    function() {
        console.log('I never get called -- nobody resolved!');
    },
    function(error) {
        console.log('I got called with', error);
    }
);

Try running that (code in reject.js)...

How does it really work?

It's complicated. Try them out for a bit and then take a look at the source for the Promise module and at the Promise/A+ specification.

Why not just use a callback?

So another way to implement this would be (code from no-promise.js):

var theyPressedEnter = function(input) {
  return input.indexOf('\n') != -1;
};

var getInput = function(callback) {
  var input = '';

  process.stdin.resume();
  process.stdin.setEncoding('utf8');

  process.stdin.on('data', function(chunk) {
    input += chunk;

    if (theyPressedEnter(chunk)) {
      input = input.replace('\n', '');
      process.stdin.pause();
      callback(input);
    }
  });
};

process.stdout.write("Enter some text and press enter: ");

getInput(function(theInput) {
  console.log("You entered: " + theInput);
});

The difference here is our code is tied to knowing it has a single callback. It is just a little messier. However, in the end, it accomplishes the exact same thing. It is important to note that promsises are not a magic bullet. They are just a way to have better separation between your code and potentially handle more complicated scenarious (multiple promises) with a syntax that is easier to read and understand. Sometimes, a simple callback is going to make more sense.

node-promise-example's People

Contributors

cymen avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.