How could one create syntactical sugar to hide some of the complexity that is .then?
Given the code below:
const Fridge = {
async openDoor() {
const myCake = new Promise(resolve => {setTimeout(()=> {
console.log('door opened')
resolve (Object.create(Cake))
}, 2000)});
await myCake
return myCake
}
}
const Cake= {
eatCake(){ console.log ( 'yummy now I can eat my cake' ) }
}
const myFridge = Object.create(Fridge)
Typically accessed via the verbatious:
myFridge.openDoor().then(myCake=>{
myCake.eatCake()
... other code here .....
}) // complicated
Can some sugar be created to instead:
myFridge.openDoor().eatCake() //simple to understand .then implied
or further, instead of:
myObject.myPromise.then(res=>{
let x = res
... do stuff with x
});
rather
let x = myObject.myPromise.res
... so stuff with x
Whatever is returned from the async function should be used for the subsequent call. And all subsequent code is assumed to be in the .then. Closure of the .then is determined by the end of the enclosing function (similar to how the await currently works).