node 笔记整理
node 合并多个请求的数据
function getList1(num) { var url= "netease/search?keyword=独孤天下&type=song&pageSize=10&page=0"; return ajaxGET(url) } function getList2(num) { return new Promise((resolve, reject) => { setTimeout(() => { resolve("haha第2个啊") }, 1100); } ) } function getList0(num) { return new Promise((resolve, reject) => { setTimeout(() => { resolve("haha第 0个啊") }, 10); } ) } async function testResult() { try { let result0 = await getList0(); // 搜索 id let result1 = await getList1();//搜索歌词 let result2 = await getList2(); // 搜索 id console.log(result0,result1,result2); } catch(err) { console.log(err); } } testResult();
eventproxy 控制并发
var ep = new eventproxy(); ep.all('data1_event', 'data2_event', 'data3_event', function (data1, data2, data3) { var html = fuck(data1, data2, data3); render(html); }); $.get('http://data1_source', function (data) { ep.emit('data1_event', data); }); $.get('http://data2_source', function (data) { ep.emit('data2_event', data); }); $.get('http://data3_source', function (data) { ep.emit('data3_event', data); });
async控制并发,可以控制并发数
ar async=require('async') var test=function(item,callback){ setTimeout(function(){ console.log(item+'ok') callback(null,item+'result') },2000) } var items=[1,2,3,4,5,7,8,9,10] async.mapLimit(items,2,function(item,callback){ test(item,callback) },function(err,result){ console.log(result) })