Last active
November 27, 2015 08:19
-
-
Save xicond/88eff3419998d91572ef to your computer and use it in GitHub Desktop.
This file contains hidden or 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
var sleep = require('sleep'); | |
var Promise = require('promise'); | |
function random (low, high) { | |
return Math.random() * (high - low) + low; | |
} | |
var task1 = new Promise(function (resolve, reject) { | |
var until = 900;//random(900,1000); | |
setTimeout(function(){ | |
for(var i=0;i<10;i++) | |
console.log('query1'); | |
resolve('query1'); | |
},until); | |
}); | |
var task2 = new Promise(function (resolve, reject) { | |
var until = 900;//random(900,1000); | |
setTimeout(function(){ | |
for(var i=0;i<10;i++) | |
console.log('query2'); | |
resolve('query2'); | |
},until); | |
}); | |
var task3 = new Promise(function (resolve, reject) { | |
var until = 900;//random(900,1000); | |
setTimeout(function(){ | |
for(var i=0;i<10;i++) | |
console.log('query3'); | |
resolve('query3'); | |
},until); | |
}); | |
Promise.all([task1,task2,task3]).then(function(queries){console.log('process all queries to output view');}).then(function(results){console.log('finish');}); | |
setTimeout(function(){console.log('Ended with setTimeout');},1); | |
sleep.sleep(1); | |
for(var i=0;i<1000000000;i++); | |
console.log('EOF'); | |
//promises async queries, reduce bottleneck | |
//console.log | |
//EOF | |
//Ended with setTimeout | |
//query2 | |
//query2 | |
//query2 | |
//query2 | |
//query2 | |
//query2 | |
//query2 | |
//query2 | |
//query2 | |
//query2 | |
//query3 | |
//query3 | |
//query3 | |
//query3 | |
//query3 | |
//query3 | |
//query3 | |
//query3 | |
//query3 | |
//query3 | |
//query1 | |
//query1 | |
//query1 | |
//query1 | |
//query1 | |
//query1 | |
//query1 | |
//query1 | |
//query1 | |
//query1 | |
//process all queries to output view | |
//finish |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Itu i di for perlu di stringify karena dia mutable. Kalau gak di stringify hasilnya pasti 10.