functiontest(callback){setTimeout(function(){var a ='1';callback(a);},500);}functiontest2(ret, callback){setTimeout(function(){var a = ret +'2';callback(a)},500);}functiontest3(ret, callback){setTimeout(function(){var a = ret +'3';callback(a);},500);}functiontest4(ret, callback){setTimeout(function(){var a = ret +'4';callback(a);},500);}functiontest5(ret){setTimeout(function(){var a = ret +'5';
console.log(a)},500);}test(function(a){test2(a,function(a1){test3(a1,function(a2){test4(a2,function(a3){test5(a3);})})});})
使用promise
functiontest(){returnnewPromise(function(resolve, reject){setTimeout(function(){var a ='1';resolve(a);},500);})}functiontest2(ret){returnnewPromise(function(resolve, reject){setTimeout(function(){var a = ret +'2';resolve(a);},500);})}functiontest3(ret){returnnewPromise(function(resolve, reject){setTimeout(function(){var a = ret +'3';resolve(a);},500);})}functiontest4(ret){returnnewPromise(function(resolve, reject){setTimeout(function(){var a = ret +'4';resolve(a);},500);})}functiontest5(ret){setTimeout(function(){var a = ret +'5';
console.log(a)},500);}test().then(function(a){returntest2(a)}).then(function(b){returntest3(b)}).then(function(c){returntest4(c);}).then(function(d){test5(d)});