Skip to content

Instantly share code, notes, and snippets.

View nem035's full-sized avatar
🐢
always learning

Nemanja Stojanovic nem035

🐢
always learning
View GitHub Profile
version: 2
jobs:
# job that installs our dependencies
# and persists them to the workspace
# so other jobs can use them
install:
working_directory: ~/enki-project
docker:
workflows:
version: 2
install_build_lint_deploy:
jobs:
- install
- build:
requires:
- install
- lint:
requires:
version: 2
jobs:
build:
working_directory: ~/enki-project
machine: true
environment:
- ENKI: 'example'
steps:
- checkout
const p = new Promise(function(resolve) {
resolve(Math.random());
});
// both of these statements give the same output
p.then(console.log);
p.then(console.log);
function all(arrayOfPromises) {
return new Promise(function (resolve, reject) {
// initialize an array of results with length of the given array of promises
const results = Array.from(Array(arrayOfPromises.length));
// since all promises are started in parallel (quickly one after the other)
// we are using a counter to keep track of when they all resolve because
// each promise can take a different amount of time to resolve
let resultsLeft = arrayOfPromises.length;
console.log("first");
// enqueue a Task after this Task
setTimeout(function() {
console.log("fourth");
}, 0);
// enqueue a microtask within this Task
Promise.resolve()
// enqueue a microtask after this microtask
console.log(1);
new Promise(function(resolve) {
console.log(2); // this gets logged right after 1
resolve();
}).then(function() {
console.log(4);
});
console.log(3);
// this
Promise.resolve(3);
// is practically the same as
new Promise(function(resolve) {
resolve(3);
});
// and this
Promise.reject(3);
// promise chains allow us to write clean vertical code
// that is much more readable
// (no need for any reject calls either because promises
// propagate errors by default)
function getThing(arg) {
return getStuff(arg)
.then(getMoreStuff)
.then(getThatFinalThing);
}
// nesting promises is an anti-pattern
// as with callbacks, we end up with a
// "pyramid of doom"
function getThing(arg) {
return new Promise(function (resolve, reject) {
getStuff(arg)
.then(function (stuff) {
getMoreStuff(stuff)
.then(function (moreStuff) {
getThatFinalThing(moreStuff)