Skip to content

Instantly share code, notes, and snippets.

@andrewmcnamara
Last active May 25, 2017 22:10
Show Gist options
  • Select an option

  • Save andrewmcnamara/159be7bf07aaac5d73a31614bff4a709 to your computer and use it in GitHub Desktop.

Select an option

Save andrewmcnamara/159be7bf07aaac5d73a31614bff4a709 to your computer and use it in GitHub Desktop.
Javascript Scratchpad (Promise concurrency)
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Testing BrowserSync</title>
</head>
<body>
<h1>Hello world</h1>
<p id="render">I'm testing BrowserSync and what it can do.</p>
<button id="btn">Fill Paragraph</button>
<script type="text/javascript" src="./js/main.js"></script>
</body>
</html>
function connThingy() {
return new Promise(function(resolve, reject) {
setTimeout(()=>{
console.log("done1");
resolve()
}
, 8000)
}
);
}
// connection
// function run(cnnection)
// function *gen() {
// yield new Promise(function(resolve, reject) {
// setTimeout(()=>{
// console.log("done1");
// resolve()
// }
// , 8000)
// }
// );
// }
// let x = gen();
// let r = x.next();
// console.log(r)
// let p4 = r.value;
// console.log("yssses")
// p4.then(()=>{
// console.log("done")
// }
// );
// // an immediately resolved promise
// var p2 = Promise.resolve("foo");
// // can get it after the fact, unlike events
// p2.then((res) => console.log(res));
// var p = new Promise(function(resolve, reject) {
// setTimeout(() => resolve(4), 2000);
// });
// // handler can't change promise, just value
// p.then((res) => {
// res += 2;
// console.log(res);
// });
// // still gets 4
// p.then((res) => console.log(res));
let n = 0;
let queue = [];
function add(job) {
queue.push(()=>{
return new Promise((resolve,reject)=>{
console.log("picking");
job();
resolve();
// setTimeout(()=>{
// console.log("done1");
// resolve()
// }
// , 8000)
});
});
next();
}
function runJob(theJob) {
n++;
theJob().then(()=>{
n--;
next();
}
);
}
function next() {
console.log("NUmber executing ",n, queue)
if (n < 20) {
let theJob = queue.pop();
if (theJob !== undefined) {
runJob(theJob);
}
}
}
function getImage(){
return new Promise(function(resolve, reject){
let url = "http://lorempixel.com/400/200/"
var img = new Image()
img.onload = function(){
resolve(url)
}
img.onerror = function(){
reject(url)
}
img.src = url
})
}
function getData2(){
let lower = 0,
upper = 10,
delay = Math.floor(lower + (Math.random() * (upper - lower + 1)));
add(()=>{fetch("https://reqres.in/api/users?delay="+10+"&thing="+Number(new Date())).then(response=>{response.json().then(function(data) {console.log(data);})})});
}
// getData();
// getData();
// getData();
console.log("Done");
// var sequence = Promise.resolve();
// for (let x=0;x<30;x++){
// sequence = sequence.then(function(){promises.push(fetch("https://reqres.in/api/users?delay="+x))});
// }
// Promise.all(promises);
[...Array(20)]
var doggypromises = [...Array(1000)].map(getData2)
// var doggypromises = [...Array(20)].map(getImage)
// Promise.all(doggypromises).then();
// async function runAll(){
// let promises = []
// for (let x=0;x<30;x++){
// http://ip.jsontest.com/
// promises.push(fetch("https://reqres.in/api/users?delay="+x).then);
// .then(response=>{response.json().then(function(data) {console.log(data);})})
// new Promise((resolve,reject)=>{ return fetch("https://reqres.in/api/users?delay=3").then(response=>{response.json().then(function(data) {console.log(data);})});})
// .then(response=>{response.json().then(function(data) {console.log(data);})});
// add(()=>{console.log(`my job ${x}`);fetch("http://ip.jsontest.com/").then(response=>{response.json().then(function(data) {console.log(data);})});});
// }
// Promise.all(promises);
// }
// async function go(){
// await runAll();
// }
// go();
// add(()=>{console.log("my job1")});
// add(()=>{console.log("my job2"); add(()=>{console.log("my job6")});});
// add(()=>{console.log("my job3")});
// add(()=>{console.log("my job4")});
// new Promise(function(resolve, reject) {
// setTimeout(()=>{
// console.log("adding job 1");
// add(()=>{console.log("my job1")});
// resolve()
// }
// , 1000)
// });
// new Promise(function(resolve, reject) {
// setTimeout(()=>{
// console.log("adding job 2");
// add(()=>{connThingy().then(()=>{console.log("my job2")})});
// resolve()
// }
// , 1)
// });
// new Promise(function(resolve, reject) {
// setTimeout(()=>{
// console.log("adding job 4");
// add(()=>{connThingy().then(()=>{console.log("my job4")})});
// resolve()
// }
// , 1)
// });
// new Promise(function(resolve, reject) {
// setTimeout(()=>{
// console.log("adding job 3");
// add(()=>{connThingy().then(()=>{console.log("my job3")})});
// resolve()
// }
// , 1)
// });
{
"name": "promising",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts":{
"start": "browser-sync start --files '*.html, js/*.js' --server"
},
"dependencies": {
"browser-sync": "^2.18.12",
"live-server": "^1.2.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment