Await iterator example discussion The `for await` expansion ```javascript= for await (const x of iterable) { foo(x); } const iterator = iterable[Symbol.asyncIterator](); const loop = () => { iterator.next().then(({value, done}) => { if (done) { return; } foo(value); loop(); }, reason => { throw reason; }); } ``` ```javascript= async function run() { if (kernelPanic) { throw kernelPanic; } let count = 0; while (!kernelKeeper.isRunQueueEmpty()) { await processQueueMessage(kernelKeeper.getNextMsg()); if (kernelPanic) { throw kernelPanic; } count += 1; } return count; } function run() { function doOne(msg) { processQueueMessage(msg).then(() => { return doOne(kernelKeeper.getNextMsg()) }) } } ``` ```javascript= let c; // purseContainer; const pursePrototype = { deposit(other) { this.balance } } c = liveslots.createContainer(pursePrototype); pC = liveslots.createContainer(paymentPrototype); function mint(initialBalance) { const me = c.create({ value: initialBalance}, CONSTTRUcTOR); return me; } return { deposit(other) { me.balance += c.get(other).balance; c.get(other).balance = 0; c.update(other, { balance: 0 }); } } } ``` ## Dean's initial version ```javascript= const purse = (me, c) => ({ deposit(other) { const otherBalance = c.get(other).balance; c.update(me, { balance: me.balance + otherBalance}); c.update(other, { balance: 0 }); } }); const c = liveslots.createContainer(purse); function mint(initialBalance) { return c.create({ balance: initialBalance}); } ``` ## Dean's explicit state version, just balance :rocket: :rocket: :rocket: Current winner ```javascript= const purse = (me, c) => ({ deposit(other) { const myBalance = c.get(me); const otherBalance = c.get(other); c.update(me, myBalance + otherBalance); c.update(other, 0); } }); const c = liveslots.createContainer(purse); function mint(initialBalance) { return c.create(initialBalance); } ``` ## Dean's "me" is external version ```javascript= const purse = (me, c) => ({ deposit(other) { const otherBalance = c.get(other).balance; const myBalance = c.get(me).balance; c.update(me, { balance: myBalance + otherBalance}); c.update(other, { balance: 0 }); } }); const c = liveslots.createContainer(purse); function mint(initialBalance) { return c.create({ balance: initialBalance}); } ``` ## Promise version ```javascript= // Dean's explicit state version, just balance, promise const purse = (me, c) => ({ async deposit(other) { const o = await other; // AWAIT ///// const myBalance = c.get(me); const otherBalance = c.get(o); c.update(me, myBalance + otherBalance); c.update(o, 0); } }); const c = liveslots.createContainer(purse); function mint(initialBalance) { return c.create(initialBalance); } ``` ```javascript= // Dean's explicit state version with just balance const purse = (me, myBalance, c) => ({ deposit(other) { const otherBalance = c.get(other); c.update(me, myBalance + otherBalance); c.update(other, 0); } }); const c = liveslots.createContainer(purse); function mint(initialBalance) { return c.create(initialBalance); } ``` ```javascript= // Dean's explicit state version const purse = (me, state, c) => ({ deposit(other) { const otherBalance = c.get(other).balance; const myBalance = state.balance; c.update(me, { balance: myBalance + otherBalance}); c.update(other, { balance: 0 }); } }); const c = liveslots.createContainer(purse); function mint(initialBalance) { return c.create({ balance: initialBalance}); } ``` Mark will think this is evil ```javascript= // Dean's property code const purse = (me, c) => ({ deposit(other) { const otherBalance = c.get(other).balance; me.balance += otherBalance; other.balance = 0; } get balance() { return me.balance; } }); const purse = (me, c) => ({ deposit(other) { const otherBalance = c.get(other).balance; c.update(me, { balance: me.balance + otherBalance}); c.update(other, { balance: 0 }); } get balance() { return me.balance; } }); const c = liveslots.createContainer(purse); function mint(initialBalance) { return c.create({ balance: initialBalance}); } ```