Created
June 11, 2014 09:11
-
-
Save jabez128/4a1d7f386989dd0acf3a to your computer and use it in GitHub Desktop.
use Object.observe to avoid callback hell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* push callback function into arr array | |
* and observe the arr | |
* pretty cool! | |
**/ | |
var request = require('request'); | |
var options = { | |
url: "http://www.baidu.com" | |
}; | |
var t1 = new Date(); | |
var arr=[]; | |
arr.push(function(err,res,body){ | |
console.log(body); | |
}); | |
Object.observe(arr,function(changes){ | |
console.log(123); | |
arr[0].apply(this,arr.slice(1)); | |
},['updated']); | |
request(options,function(){ | |
var args = [].slice.call(arguments); | |
args.map(function(item){ | |
arr.push(item); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment