Published
Edited
Oct 30, 2019
2 forks
1 star
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
{
var romeo = {name: "romeo", hasRose: false}
var juliet = {name: "jules", wantsRose: true}
if(romeo.hasRose===true){
romeo.hasRose=false
juliet.wantsRose=false
return "Happily ever after!"
}
else if(romeo.hasRose===false)
{
return "Romeo fails"
}
romeo.hasRose=true
}
Insert cell
Insert cell
{
var romeo = {name: "romeo", hasRose: false}
var juliet = {name: "jules", wantsRose: true}
if(romeo.hasRose===true){
romeo.hasRose=false
juliet.wantsRose=false
return "Happily ever after!"
}
else if(romeo.hasRose===false)
{
romeo.oweRose=true
juliet.wantsRose=false
}
romeo.hasRose=true
romeo.oweRose=false
romeo.hasRose=false
return "Happily ever after!"
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
promiseFunc = () => new Promise((resolve, reject) => {setTimeout(() => resolve(x+50), 1000)})
Insert cell
finalResult = (promiseFunc())
.then(x => x%2)
.then(x => x+30)
.catch(err => "I encountered an error: " + err + ". Sorry!")
Insert cell
Insert cell
Insert cell
youPromiseToGiveMeFood = typeOfFood => new Promise( (s,f) => s(typeOfFood))
Insert cell
iPromiseToPayYouBack = (amt, from) => new Promise( (s,f) => s(from - amt) )
Insert cell
Insert cell
eatFood = myStuff => "I ate the " + myStuff.food
Insert cell
checkWhatIHave = myStuff => "I have a " + myStuff.food + " and $" + myStuff.wallet + " dollars left."
Insert cell
// define the async function
foodTransaction = async function() {
var myFood;
var myWallet = 100;

// the function will pause at this line below until you give me my food
myFood = youPromiseToGiveMeFood("Lakeview Beef w. Broccoli")
// then it'll keep executing down here
// and pause until i pay you back
myWallet = /* Promise */ iPromiseToPayYouBack(20, myWallet)
// and now we've finished the transaction
// I return an object containing my belongings
return {
food:myFood,
wallet: myWallet
}
}
Insert cell
Insert cell
foodTransaction() instanceof Promise
Insert cell
foodTransaction().then(eatFood)
Insert cell
foodTransaction().then(checkWhatIHave)
Insert cell
Insert cell
Insert cell
func = (x) => undefined
Insert cell
{ //Your code is checked here
expect(func()).to.be.an.instanceOf(Promise)
expect(func("bob").catch(err => err)).to.be.an.instanceOf(Error)
expect(func(null).catch(err => err)).to.be.an.instanceOf(Error)
expect(func().catch(err => err)).to.be.an.instanceOf(Error)
return success()
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more