Skip to content

Instantly share code, notes, and snippets.

@jabez128
Created June 11, 2014 09:11
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 jabez128/4a1d7f386989dd0acf3a to your computer and use it in GitHub Desktop.
Save jabez128/4a1d7f386989dd0acf3a to your computer and use it in GitHub Desktop.
use Object.observe to avoid callback hell
/**
* 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