Neues Node.js-Buch

Check type of callback parameter

Problem

You want to be sure that the value passed as the callback parameter is really a function.

Ingredients

  • the typeof operator
  • the === operator
  • optional: the toString() and the call method

Directions

  1. Given: a function that accepts a callback function.

    function someFunction(callback) {
      callback();
    }
Weiterlesen

Create a delaying function in promise-style

Problem

You want to create a function that creates a promise that delays for a given number of milliseconds.

Ingredients

  • a function
  • the Promise object
  • the setTimeout() function

Directions

  1. Create a function that accepts the number of milliseconds.

    function delay(ms) {
      ...
    }
Weiterlesen

Create a generic function for partial application

Problem

You want to create a generic function, that produces for a given function the partial applied function.

Ingredients

  • 2 functions
  • the apply() method
  • the slice() method

Directions

  1. Define a function that accepts another function (the one that should be partial applicable).

    function partial(fn /*, args...*/) {
      ...
    }
Weiterlesen