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
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: |
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
workflows: | |
version: 2 | |
install_build_lint_deploy: | |
jobs: | |
- install | |
- build: | |
requires: | |
- install | |
- lint: | |
requires: |
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
version: 2 | |
jobs: | |
build: | |
working_directory: ~/enki-project | |
machine: true | |
environment: | |
- ENKI: 'example' | |
steps: | |
- checkout |
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
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); |
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
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; |
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
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 |
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
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 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
// this | |
Promise.resolve(3); | |
// is practically the same as | |
new Promise(function(resolve) { | |
resolve(3); | |
}); | |
// and this | |
Promise.reject(3); |
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
// 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); | |
} |
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
// 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) |