Skip to content
home@dennisPersonal Website of Dennis Plöger
  • Home
  • Blog
  • Artist
  • Impressum
  • Datenschutzerklärung

Promising to node-rest-client

10. Februar 2017 2 comments Article Allgemein

I’m currently in love with Promises. Somehow, only now I get the main concept behind that and the benefits. Mainly, because most of the documentation is a bit too detailed about that.

Let me sum it up for you:

If you’re developing asynchronous Javascript (which you should be, when you’re actually developing Javascript!), most of the time you’re passing callback functions to asynchronous functions:

function async(callback) {
  // do something
  if (there_is_an_error) {
    callback(error);
  } else {
    callback(null, { something: 'return' }); // everything's fine
  }
}

function callAsync() {
  async(
    (error) => {
      if (error) {
        // handle error
      } else {
        // keep on working
      }
    }
  );
}

This gets very ugly if you’re chaining multiple callbacks after another. There are helpers for that – for example the wonderful Async.js (and that’s the first choice when it comes to asynchronous calls in loops), but it still gets ugly.

Promises, however, allow you to chain callbacks and handling errors in a much more cleaner way:

function async() {
  return new Promise(function (fulfill, reject) {
    // do something
    if (there_is_an_error) {
      reject(error);
    } else {
      fulfill({ something: 'return' }); // Everything's fine
    }
  });
}

function callAsync() {
  async()
    .catch(
      (error) => {
        // handle error
      }
    )
    .then(
      () => {
        // keep on working
      }
    );
}

Well, this example is not that good to show you the benefits, but think about five callbacks in a row. If you’re using a column limit in your code conventions, you’ll be effectively writing on the right border of your screen. Not with Promises.

To wrap this post up, I have a present for all folks using node-rest-client. I’ve made a Promise wrapper around it using Bluebird and I’ve called it node-rest-client-promise. Just grab it using „npm install node-rest-client-promise –save“ instead of node-rest-client and use it like this:

var nodeRestClient = require('node-rest-client-promise');

var client = nodeRestClient.client({
  // node-rest-client.Client-options 
});

client.getPromise(
  'https://www.google.de'
).catch(
  (error) => {
    // there was an error
  }
).then(
  () => {
    // everything's great.
  }
);

Have fun.

PDF
Tags: javascript, node, promises, rest

2 comments

  • Jeff Dickens 18. Februar 2018 at 18:57 Antworten

    Greetings. I’ve tried out node-rest-client-promise but found I need to be able to use the registerMethod functionality in node-rest-client. If this were ever added to node-rest-client-promise I’d be grateful.

    • Dennis 18. Februar 2018 at 19:28 Antworten

      Hi!

      Would you mind opening an issue over at GitHub for that?

      Thanks!

      Kind regards
      Dennis

Schreibe einen Kommentar Antworten abbrechen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Diese Seite verwendet Akismet, um Spam zu reduzieren. Erfahre, wie deine Kommentardaten verarbeitet werden..

Calendar

Mai 2025
M D M D F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« Jan.    

Archive

  • Januar 2024
  • Dezember 2023
  • April 2021
  • März 2021
  • September 2020
  • Dezember 2019
  • November 2019
  • Oktober 2019
  • Juli 2019
  • Juni 2019
  • Mai 2019
  • April 2019
  • März 2019
  • September 2018
  • August 2018
  • Juli 2018
  • März 2018
  • Januar 2018
  • Dezember 2017
  • September 2017
  • März 2017
  • Februar 2017
  • Januar 2017
  • August 2016
  • Mai 2016
  • Dezember 2015
  • November 2015
  • August 2015
  • März 2015
  • Dezember 2014
  • September 2014
  • August 2014
  • Juli 2014
  • Februar 2014
  • Oktober 2013
  • September 2013
  • August 2013
  • Juli 2013
  • Juni 2013
  • Mai 2013
  • April 2013
  • November 2012
  • Oktober 2012
  • September 2012
  • August 2012
  • Juni 2012
  • Mai 2012
  • März 2012
  • Februar 2012
  • Januar 2012
  • November 2011
  • Juli 2011
  • Juni 2011
  • März 2011
  • Februar 2011
  • Januar 2011
  • Dezember 2010
  • November 2010
  • April 2010
  • Februar 2010

Kategorien

  • Allgemein

Neueste Beiträge

  • Making Puppet host reports in Foreman available in Grafana
  • Introducing Handler Inspector
  • Github Action for the Godot Asset library
  • Announcing EgoVenture
  • The third installment of my learning project

Schlagwörter

administration apache blog commit configuration configuration management development dhcp django documentation dokuwiki extension html ip java javascript keyboard lbows linux log migration netdot networks news php plugin printer python rest server shortcut skd sles soap social ssh survey twitter ubuntu udev vmware website zimbra zimlet zimlets

Meta

  • Anmelden
  • Feed der Einträge
  • Kommentar-Feed
  • WordPress.org

Copyright (c) Dennis Plöger | Theme by ThemeinProgress | Proudly powered by WordPress

Zum Ändern Ihrer Datenschutzeinstellung, z.B. Erteilung oder Widerruf von Einwilligungen, klicken Sie hier: Einstellungen