Giter Site home page Giter Site logo

showcase2's Introduction

异步

那些年我们都沉浸在 Callback Hell 的恐惧中

如果你手贱,你还能找到一个叫 回调地狱 的网站

fs.readdir(source, function (err, files) {
  if (err) {
    console.log('Error finding files: ' + err)
  } else {
    files.forEach(function (filename, fileIndex) {
      console.log(filename)
      gm(source + filename).size(function (err, values) {
        if (err) {
          console.log('Error identifying file size: ' + err)
        } else {
          console.log(filename + ' : ' + values)
          aspect = (values.width / values.height)
          widths.forEach(function (width, widthIndex) {
            height = Math.round(width / aspect)
            console.log('resizing ' + filename + 'to ' + height + 'x' + height)
            this.resize(width, height).write(dest + 'w' + width + '_' + filename, function(err) {
              if (err) console.log('Error writing file: ' + err)
            })
          }.bind(this))
        }
      })
    })
  }
})

我一直觉得回调地狱除了整个逻辑不太清晰以外,最大的问题是:我TM不知道是不是有地方少了一个大括号!!!

然后 Promise 出现了

因为我不是考古派,我们也不讨论 Promise 的历史了。

NodeJS 的 Stream

Steam 是 NodeJS 的核心编程模型,James Halliday 写过一本指南 stream-handbook

Generator

然后 ES6 Generator 出现了,然后大家发现这玩意的特性能做异步流程控制 Generator 函数的含义与用法

Async Function

Generator 这个异步写起来能么爽,干嘛不做个原生实现呢?于是 Async Function 就出现了

将管道模型用在前端

RxJS

用例运行

npm run 1

npm run 2

....

showcase2's People

Watchers

James Cloos avatar jine 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.