Skip to content

Instantly share code, notes, and snippets.

@closetothe
closetothe / delay.js
Created November 17, 2019 07:40
A simple delay function for async javascript
const delay = ms => {
return new Promise(resolve => {
setTimeout(() => resolve(), ms);
});
};
export default delay;
// example (1-second delay)
// await delay(1000)
@closetothe
closetothe / postPriority.js
Last active July 6, 2019 17:15
A simple recursive function for flattening a tree of forum posts saved in MongoDB, preserving the correct order in which to display them (left order traversal). Used in notmoodle.com (app.js and thread.ejs).
var getPostPriority = async function(nodeRef, priority){
var node = await Post.findById(nodeRef)
priority.push(node);
for(var i = 0; i < node.children.length; i++)
await getPostPriority(node.children[i], priority);
return priority;
}