Last active
June 1, 2020 18:52
In order and In Sync (high performance) JS
This file contains 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
// Immediate execution, for of with indexes, in order | |
const r = ["a",2,3,4,5] | |
const t = (i) => { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve(`hi ${i}`) | |
}, 1000 * i) | |
}) | |
} | |
!async function() { | |
for(const [i, e] of r.entries()) { | |
const q = await t(1) | |
console.log(e, q) | |
} | |
}() | |
(async () => { | |
/* */ | |
})() | |
// Run async with sync inside | |
const po = () => { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
resolve("hi") | |
}, 2000) | |
}) | |
} | |
const zz = ["hey", "hi", "ho"] | |
await Promise.all(zz.map(async (ele) => { | |
const y = await po() | |
console.log(y) | |
return ele | |
})) | |
.then((balues) => { | |
console.log(balues) | |
for (val of balues) { | |
console.log('eee') | |
} | |
}) | |
// Similar to above: | |
// Upload step images | |
const stepimageProms = [] | |
const stepimageArr = Array(this.state.steps.length) | |
for (let i = 0; i < this.state.steps.length; i++) { | |
if (this.state.steps[i].image) { | |
const fileName = `images/${uuid()}-${this.state.steps[i].image.name}` | |
const imageRef = storageRef.child(fileName) | |
await stepimageProms.push( | |
await imageRef.put(this.state.steps[i].image) | |
.then(async (uploadSnap) => { | |
const downloadURL = await imageRef.getDownloadURL() | |
stepimageArr[i] = downloadURL | |
}) | |
.catch((e) => { | |
console.error('Error uploading step iomage:') | |
console.error(e) | |
}) | |
) | |
} | |
} | |
// Handle steps | |
Promise.all(stepimageProms) | |
.then(() => {}) | |
// Manual delay of impossible async stuff: | |
!function(){ | |
for (const [i, e] of r.entries()) { | |
setTimeout(() => { | |
// some impossible async thing that await is just not working on | |
console.log(e) | |
}, 1000*i) // maunally delay firing it off | |
} | |
}() | |
// Batching groups of promises for doing large arrays of promises quickly in groups of 10 | |
let final = [] | |
let batch = [] | |
for (let i = 0; i<devices.length + 11; i++) { // make sure we get them all | |
const dev = devices[i] | |
if (i%10 == 0 && i > 1) { | |
batch.push(dev) // don't skip every 10 | |
console.log("doing 10") | |
await Promise.all(batch.map(async (ele) => { // async inside so we've read before we return | |
console.log("doing the lords work") | |
await db.collection("clients").doc(cuid).collection("devices").doc(ele).get() | |
.then((docSnap) => { | |
let infoObj = docSnap.data() | |
infoObj.duid = ele | |
final.push(infoObj) | |
}) | |
.catch((err) => { | |
console.error("Error getting devices from campaign") | |
console.error(err) | |
reject(err) | |
}) | |
return (ele) | |
})) | |
console.log("did 10:") | |
batch = [] // clear the batch | |
} else { | |
batch.push(dev) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment