Skip to content

Instantly share code, notes, and snippets.

@karenpeng
Created August 22, 2015 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karenpeng/1afa9c2cceb75c324ec1 to your computer and use it in GitHub Desktop.
Save karenpeng/1afa9c2cceb75c324ec1 to your computer and use it in GitHub Desktop.
var Mocha = require('mocha');
var assert = require('assert');
var mocha = new Mocha({ui: 'bdd'});
/*
Your previous Plain Text content is preserved below:
// 实现一个函数 sequence,参数是一个数组,是一系列的异步函数,返回一个新的函数,这个新的函数在调用的时候,会顺序的执行之前传递给 sequence 的函数数组,前一个函数的返回值作为下一个函数的参数,新函数的参数前面部分会传递给函数数组的第一个函数,当全部执行完成后,调用 新函数的 callback。
sequence([add1, add2])(1, function (err, res) {
console.log('hahahahah', res) // 4
})
function add1(input, callback) {
setTimeout(callback.bind(null, null, input + 1), 1000);
}
function add2(input, callback) {
setTimeout(callback.bind(null, null, input + 2), 1000);
}
/**
@param array
@return function
**/
function sequence(arrOfFunc){
// 注意参数,测试用例里面的参数是不固定的喔,需要自己判断到底第几个是 callback
// 一般来说我们都会约定一个函数的最后一个参数为 callback,同时 callback 一定是接收两个参数(err, data)
// 测试用例里面也体现了这一点
var tasks = Array.prototype.slice.call(arguments)
tasks = flatten(tasks)
return function test(){
var cb = arguments[arguments.length-1]
if(arrOfFunc === undefined ||
arrOfFunc === null ||
arrOfFunc.length === 0){
return cb(null, null)
}
var params = Array.prototype.slice.call(arguments)
var arr = params.slice(0, params.length-1)
if(tasks.length === 0){
return cb.apply(null, [null].concat(arr))
}
var cur = tasks.shift()
if(typeof cur !== 'function'){
return cb(new Error('input functions invalid'))
}
var callback = function(err, res){
if(err) return cb(err)
test(res, cb)
}
cur.apply(null, arr.concat([callback]))
}
}
function test(){
return function(){
console.log(arguments)
}
}
test(1)()
function test1(){
var a = Array.prototype.slice.call(arguments);
console.log(Array.isArray(a))
}
function test2(){
return function(){
}
}
test1(1,2)
test1([1,2])
// for(var i = 0; i < 5; i++){
// setTimeout(function(){
// //console.log('why' , i , 'not 4')
// console.log(i)
// }, 100 * i)
// }
//wow, there's no blocking here
// for(var i = 0; i < 5; i++){
// (function(i){
// setTimeout(function(){
// console.log(i)
// }, 100 * i)
// })(i)
// }
for (var i = 0; i < 5; i++) {
console.log(i);
}
console.log('for end, now i is ', i)
function flatten(arr){
var result = []
arr.forEach(function(e){
if(Array.isArray(e)){
var a = flatten(e)
result = result.concat(a)
}else{
result.push(e)
}
})
return result
}
function add(num1, num2, callback) {
setTimeout(function () {
callback(null, num1 + num2)
}, 10)
}
function add1(num, callback) {
setTimeout(function () {
callback(null, num + 1)
}, 10)
}
function error(num, callback) {
setTimeout(function () {
callback(new Error('oops'))
}, 10)
}
// Bit of a hack, sorry!
mocha.suite.emit('pre-require', this, 'solution', mocha);
describe('test flatten', function(){
describe('when given a mixed arguments', function(){
it('should return a flatten array', function(done){
assert(flatten([1,[2,[3]]])) === [1,2,3]
done()
})
})
})
describe('sequence', function() {
describe('when input function array is undefined', function () {
it('should callback null', function (done) {
sequence()(function (err, res) {
assert(err === null)
assert(res === null)
done()
})
})
})
describe('when input function array is empty', function () {
it('should callback null', function (done) {
sequence([])(function (err, res) {
assert(err === null)
assert(res === null)
done()
})
})
})
describe('when input functions in arguments', function () {
it('should callback with result', function (done) {
sequence(add, add1)(1, 1, function (err, res) {
assert(err === null)
assert(res === 3)
done()
})
})
})
describe('when input functions in an array', function () {
it('should callback with result', function (done) {
sequence([add, add1])(1, 1, function (err, res) {
assert(err === null)
assert(res === 3)
done()
})
})
})
//hey what should this do???
// dead_horse:这个是我写的么?
//不,我写的
describe('when input functions in an array', function () {
it('should callback with result', function (done) {
sequence(add, [add1])(1, 1, function (err, res) {
assert(err === null)
assert(res === 3)
done()
})
})
})
describe('when input functions in an array', function () {
it('should callback with result', function (done) {
sequence([add], add1)(1, 1, function (err, res) {
assert(err === null)
assert(res === 3)
done()
})
})
})
describe('when error happened', function () {
it('should callback with error', function (done) {
sequence([add1, error])(1, function (err, res) {
assert(err.message === 'oops')
done()
})
})
})
describe('when non-function elements in input arrays', function () {
it('should callback with an error', function (done) {
sequence([add, 1, error])(1, 1, function (err, res) {
assert(err.message === 'input functions invalid')
done()
})
})
})
});
mocha.run(function() {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment