GithubHelp home page GithubHelp logo

puyanwei / waterfall-function-workshop Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rhodespeter/waterfall-function-workshop

0.0 1.0 0.0 14 KB

Morning challenge workshop for Founders & Coders 10

waterfall-function-workshop's Introduction

Morning challenge: implement a waterfall function

This workshop has been inspired by Eoin McCarthy and his legendary waterfall function workshop given to FAC9 by Besart Hoxhaj.

You may have seen the compose function before, it allows you to take some functions and compose them into a single function! What do you do if the functions are asynchronous? You implement the waterfall function.

Like the compose function, the waterfall function runs an array of functions in series, each passing their result to the next function in the array. Crucially, the waterfall function will not invoke the next function in the array until the current function being run has been returned.

Waterfall function:

The waterfall function takes three arguments:

  1. An initial argument, this is passed into the first async function in the waterfall.

  2. The second argument is an array of functions, each function takes two arguments. The first argument it uses to find a result and the second is a callback function.

  3. A final callback is given as the third argument. This is called after the waterfall has invoked all of the functions in the array.

Workshop:

  1. Copy the code below and paste it into Repl.

  2. Create the waterfall function, pass the tests.

  3. For bonus points, create some additional tests for the waterfall function.

function asyncAddOne(x, callBack) {
  setTimeout(function() {
    if (typeof x !== 'number'){ return callBack(new Error('need a number!')) }
    else { return callBack(null, x + 1) }
  }, 200)
}

function asyncDouble(x, callBack) {
  setTimeout(function() {
    if (typeof x !== 'number') { return callBack(new Error('need a number!')) }
    else { return callBack(null, x * 2) }
  }, 200)
}

function asyncTimesTen(x, callBack) {
  setTimeout(function() {
    if (typeof x !== 'number') { return callBack(new Error('need a number!')) }
    else { return callBack(null, x * 10) }
  }, 200)
}

// Create this function!
function waterfall(arg, tasks, cb) {
  cb(new Error('waterfall function not implemented'));
}

waterfall(3, [
  asyncAddOne,
  asyncDouble,
  asyncTimesTen
], function(error, result) {
  console.log('Test 1');
  if (error) {
    console.log('test failed, ' + error)
  }
  else if (result !== 80) {
    console.log('test failed, expected 80 but got', result)
  }
  else {
    console.log('Test 1 passed!')
  }
})

Glossary:

waterfall-function-workshop's People

Contributors

rhodespeter avatar

Watchers

 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.