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

miss tyni example with .then().catch() #118

Open
maboiteaspam opened this issue Dec 23, 2015 · 1 comment
Open

miss tyni example with .then().catch() #118

maboiteaspam opened this issue Dec 23, 2015 · 1 comment

Comments

@maboiteaspam
Copy link

TLDR; I believe an example like this should be added to the README,

P().then(function (port) { throw 'boom!';}).catch(console.error)

Hi,

I ve decided today to start learning promise, i was still stuck into streams ; 0

I would just like to share with you my little experiment to improve the README.

I wrote some shit like this

var Promise = require('promise');

module.exports = function (port, host) {

  var p =  ((port) && Promise.denodeify(function(then){then(null, port)}) ) || require('get-port');

  return p().then(function (port){
    if (!(port).match(/[^:]+:[^:]+/)) port = host + ':' + port;
    return port;
  })
}

Which bugs in if (!(port).match(/[^:]+:[^:]+/)) port = host + ':' + port; because port is an int, but match belong to String.

Anyway, the fact is the error was caught by the promise, and did not produce any output.

So i was kind of like wtf ?! for a while...

Maybe i misread the README and missed something, but i believe it worth to show somewhere something like

P().then(function (port) { throw 'boom!';}).catch(console.error)

just to let me people come in and fail in a friendly way while learning and developing.

@ForbesLindesay
Copy link
Member

What you're looking for is .done(). So the example would be:

P().then(function (port) { throw 'boom!';}).done();

.done can also be used instead of .then when you don't plan to do anything with the returned promise. Think of .then like array.map and .done like array.forEach.

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

2 participants