Last active
July 28, 2022 04:55
-
-
Save trungnghia112/f3a0f0b01499d6d5983a8e169852b82b 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
downloadObjectAsJson(exportObj: any, exportName: any = Date.now()) { | |
var dataStr = "data:text/json;charset=utf-8," + encodeURIComponent(JSON.stringify(exportObj)); | |
var downloadAnchorNode = document.createElement('a'); | |
downloadAnchorNode.setAttribute("href", dataStr); | |
downloadAnchorNode.setAttribute("download", exportName + ".json"); | |
document.body.appendChild(downloadAnchorNode); // required for firefox | |
downloadAnchorNode.click(); | |
downloadAnchorNode.remove(); | |
} | |
/** | |
* const data = await this.fileToJSON(event.files[0]); | |
* @param file | |
* @returns | |
*/ | |
async fileToJSON(file: any) { | |
return new Promise((resolve, reject) => { | |
const fileReader = new FileReader() | |
fileReader.onload = (event: any) => resolve(JSON.parse(event.target.result)) | |
fileReader.onerror = error => reject(error) | |
fileReader.readAsText(file) | |
}) | |
} | |
# async/await inside loops | |
# You could also use while or do..while or for loops too with this same structure. | |
# But you can’t await with Array.forEach() or Array.map() | |
## Reading Promises in sequence | |
function testPromise(time) { | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
console.log(`Processing ${time}`); | |
resolve(time); | |
}, time); | |
}); | |
} | |
let result = [3000,2000,1000, 4000].reduce( (accumulatorPromise, nextID) => { | |
return accumulatorPromise.then(() => { | |
return testPromise(nextID); | |
}); | |
}, Promise.resolve()); | |
result.then(e => { | |
console.log("All Promises Resolved !!✨") | |
}); | |
## Resolving Promises in parallel | |
async function printFiles() { | |
let fileNames = ['picard', 'kirk', 'geordy', 'ryker', 'worf']; | |
await Promise.all(fileNames.map(async (file) => { | |
const contents = await fs.readFile(file, 'utf8'); | |
console.log(contents); | |
})); | |
} | |
## Use the for..of | |
const fun = (prop) => { | |
return new Promise(resolve => { | |
setTimeout(() => | |
resolve(`done ${prop}`), 1000); | |
}) | |
} | |
const go = async () => { | |
const list = [1, 2, 3] | |
for (const prop of list) { | |
console.log(prop) | |
console.log(await fun(prop)) | |
} | |
console.log('done all') | |
} | |
go() | |
## Use the for..of | |
const fun = (prop) => { | |
return new Promise(resolve => { | |
setTimeout(() => | |
resolve(`done ${prop}`), 1000); | |
}) | |
} | |
const go = async () => { | |
const obj = { a: 1, b: 2, c: 3 }; | |
for (const prop in obj) { | |
console.log(prop) | |
console.log(await fun(prop)) | |
} | |
console.log('done all') | |
} | |
go() | |
----------------------------------------- | |
#Lazy load any charts and ads with lazysizes.js | |
<div id="tracker_chart" class="chart lazyload" data-expand="-150" style="height: 510px;"></div> | |
<script type="text/javascript"> | |
document.getElementById("tracker_chart").addEventListener("lazybeforeunveil", function() { | |
var scriptLibrary = document.createElement('script'); | |
scriptLibrary.onload = function () { | |
var scriptChart = document.createElement('script'); | |
scriptChart.src = '/js/chart.js'; | |
document.body.appendChild(scriptChart); | |
}; | |
scriptLibrary.src = 'https://cdn.jsdelivr.net/npm/[email protected]/dist/apexcharts.min.js'; | |
document.head.appendChild(scriptLibrary); | |
}); | |
</script> | |
## To lazy load ads | |
<script type="text/javascript"> | |
window.addEventListener('DOMContentLoaded', (event) => { | |
if (window.innerWidth<992){ | |
/*your functions for big screen*/ | |
document.getElementById("widget_ad_mobile").addEventListener("lazybeforeunveil", function() { | |
var scriptAds = document.createElement('script'); | |
scriptAds.onload = function () { | |
(adsbygoogle = window.adsbygoogle || []).push({}); | |
}; | |
scriptAds.src = '[https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'](https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'); | |
document.getElementById("widget_ad_mobile").appendChild(scriptAds); | |
}); | |
} | |
}); | |
</script> | |
#Capitalize the first letter of each word | |
function titleCase(str) { | |
var splitStr = str.toLowerCase().split(' '); | |
for (var i = 0; i < splitStr.length; i++) { | |
// You do not need to check if i is larger than splitStr length, as your for does that for you | |
// Assign it back to the array | |
splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1); | |
} | |
// Directly return the joined string | |
return splitStr.join(' '); | |
} | |
document.write(titleCase("I'm a little tea pot")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment