<snapdata remixID="9394190"><project name="Promises" app="Snap! 5.4, http://snap.berkeley.edu" version="1"><notes>An implementation of promises in Snap!.</notes><thumbnail>data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAKAAAAB4CAYAAAB1ovlvAAAFk0lEQVR4nO3YXUxTdxzGce4Wd7vtYk6uluzCiyXLlnizRWd2ZcySmXhjvACnhCbODWoYG0JRkC3BNTaQqGMHqCwyQgoelBe1O0BTz7Ar4+QELWjHSwgYzRa3obx48+zCcTKySTWTPFnzfJJfSMu/4Rf+XwIlByJEOX9/sLi4iHQ6jZs3b9Lm7t27rO+FEHgBxmIxDA8PI51OP9OZm5vF2RNHUFfpX3NO13yGyckJpNNp9Pf34/bt296So6Oj6O7uXpeJRqOwbTvj9PZ0o/viRViWhampKcplZaMc4NEF37hxY13m2xNlSPk24lf/powz1XbMe10sFgMAuK6LsbGxdZl0Oo1TH72P1g9eRNuutac771VMTvyMsbExXLt2TRE+IzkA4DiON4WFhdiyZQsOHz4MwzBQWloKx3FgGAZ8Ph8Mw/DG5/PB5/OhsLBw1fmVzzuOg9bjB/EgtAMPf+rAQsMeLF8OYvlyEA9jX2O+JBfLVj3mS3IxX5KL3y/UeHtYlgUAaGtrg+u6cF0XtbW1qK6u9h4/6RiGgYmJiX88Pz4+jm92vuB9/Uxz584d77WO41AvLlvkAMDw8DASiQQSiQQKCgoQCARQWVmJQCCAQCCA6elpHDhwwHtu5WNFRQXKy8tRVlaGYDDonS8rK8Ps7CwSiQRajx/EHyW5WOr98lFwl4NYjp7E8vd1eFD7Lha/+8S74N+6jnt7rATY0tKCZDKJZDKJqqoq5OXl4ejRo9i+fTv8fj+SySTy8/Nx6NAh7N27F8XFxQiFQti/fz/Ky8vh9/tRVFSEQCCAbdu2oaCgAFu3bkUqlcL169fR+BQBzs3NebuMjIxQLy5b5ABANBpFPB5HPB5HX18f8vPzEQ6H4bouzpw5g5GREdTV1SEcDsO2bezbtw/hcBjNzc1oampCU1MTioqKvPONjY1wXRfxeNwLcOHULsyX5GLh9G7cr3wdC6d340FoB+5Xveld8D2z2tujoaEBAGAYhvd3WE9PD65evYqOjg6cP38epmnCtm3k5eXBNE1cunQJ9fX1sCwLnZ2d6OzsRCQSQSQSgWmaME0Tra2tuHLlCmzbhuM4TxXgzMyMt8vQ0BD14rKF9ybk3LlzsCzrmU9/pBlLf/3azTTdp47BsixEIhGkUikAgG3bGBgYWHPa29sznvnX6e9H7MiOJwuwYjOS14YwMDAA0zRx69Yt2qVlk1X/hunt7cXg4CBisRhtotEoxsfHVy0Zj8cRCoXWZU5+VYuurq6MU38yiE+LP4ZhGN4Ph/x3OZmPiKwfBShUClCoFKBQKUChUoBCpQCFSgEKlQIUKgUoVApQqBSgUClAoVKAQqUAhUoBCpUCFCoFKFQKUKgUoFApQKFSgEKlAIVKAQqVAhQqBShUClCoFKBQKUChUoBCpQCFSgEKlQIUKgUoVApQqBSgUClAoVKAQqUAhUoBCpUCFCoFKFQKUKgUoFApQKFSgEKlAIVKAQqVAhQqBShUClCoFKBQKUChUoBCpQCFSgEKlQIUKgUoVApQqBSgUClAoVKAQqUAhUoBCpUCFCoFKFQKUKgUoFApQKFSgEKlAIVKAT5GTU0NlpaW2GtkPQX4GKnYAOY3bcCF1zYCwWqYLWfZK2UlBbiGXy52ArnPe3PvlQ3oeOctzNRWwbZt9npZQQFm4GzetCrClel96Tn8+LkfqVSKveL/mgJcQ/vO97zgJl/egNEP9+CHL45henqavVrWUICPESn1o+/tN3DvRDUGBwfZ62QtBShUClCoFKBQKUChUoBCpQCFSgEKlQIUKgUoVApQqBSgUClAoVKAQqUAhUoBCpUCFCoFKFQKUKgUoFApQKFSgEKlAIVKAQqVAhQqBShUClCoFKBQKUChUoBCpQCFSgEKlQIUKgUoVApQqP4EYXOnCzFDtEUAAAAASUVORK5CYII=</thumbnail><stage name="Stage" width="480" height="360" costume="0" color="255,255,255,1" tempo="60" threadsafe="false" penlog="false" volume="100" pan="0" lines="round" ternary="false" codify="false" inheritance="true" sublistID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</pentrails><costumes><list struct="atomic" id="2"></list></costumes><sounds><list struct="atomic" id="3"></list></sounds><variables></variables><blocks></blocks><scripts></scripts><sprites><sprite name="Sprite" idx="1" x="0" y="-70" heading="90" scale="1" volume="100" pan="0" rotation="1" draggable="true" costume="0" color="255,29,10,1" pen="tip" id="8"><costumes><list struct="atomic" id="9"></list></costumes><sounds><list struct="atomic" id="10"></list></sounds><blocks></blocks><variables></variables><scripts><script x="20" y="20"><custom-block s="promise %repRing"><block s="reifyScript"><script></script><list></list></block><comment w="396" collapsed="false">Launches a computation and reports a value representing it. This value is implemented as a list that gets updated when the computation has finished.</comment></custom-block></script><script x="20" y="89"><custom-block s="%l has completed?"><l/><comment w="396" collapsed="false">Reports true if the given computation has finished.</comment></custom-block></script><script x="20" y="146"><custom-block s="after %l do %repRing"><l/><block s="reifyReporter"><script></script><list></list></block><comment w="396" collapsed="false">Schedules another computation to perform on the result of the given computation. Reports a promise representing the new computation.</comment></custom-block></script><script x="20" y="215"><custom-block s="wait for %l"><l/><comment w="396" collapsed="false">Waits for the given computation to complete, and reports its result. If the computation has already finished, it simply reports the result that has already been computed.</comment></custom-block></script><script x="20" y="296"><block s="receiveGo"></block><block s="setColor"><color>255,29,10,1</color></block><block s="gotoXY"><l>0</l><l>-70</l></block><block s="setHeading"><l>90</l></block><block s="doSetVar"><l>the number</l><l>1187</l></block><block s="doSetVar"><l>the promise</l><l></l></block><block s="doSayFor"><l>This is an example program that shows how promises work!</l><l>3</l></block><block s="doSayFor"><block s="reportJoinWords"><list><l>I will determine whether </l><block var="the number"/><l> is a prime... while dancing!</l></list></block><l>3</l></block><block s="doSetVar"><l>the promise</l><custom-block s="(promise) is %n prime or composite?"><block var="the number"/></custom-block></block><block s="doSayFor"><l>My prime procedure has returned a promise!</l><l>3</l></block><block s="doSayFor"><block s="reportJoinWords"><list><l>It has also launched a procedure that is currently checking if </l><block var="the number"/><l> is prime!</l></list></block><l>3</l></block><block s="doSayFor"><l>When it has finished, it will update the promise, and I will stop dancing!</l><l>3</l></block><block s="doUntil"><custom-block s="%l has completed?"><block var="the promise"/></custom-block><script><block s="turn"><l>15</l></block><block s="changePenHSVA"><l><option>hue</option></l><l>10</l></block></script></block><block s="doSayFor"><l>It&apos;s done!</l><l>2</l></block><block s="doSayFor"><block s="reportJoinWords"><list><l>Looks like </l><block var="the number"/><l> is a </l><custom-block s="wait for %l"><block var="the promise"/></custom-block><l> number!</l></list></block><l>2</l></block></script></scripts></sprite><watcher var="the number" style="normal" x="4" y="5.000001999999995" color="243,118,29"/><watcher var="the promise" style="normal" x="120" y="6" color="243,118,29"/></sprites></stage><hidden></hidden><headers></headers><code></code><blocks><block-definition s="promise %&apos;computation&apos;" type="reporter" category="other"><header></header><code></code><translations></translations><inputs><input type="%repRing"></input></inputs><script><block s="doDeclareVariables"><list><l>promise</l></list></block><block s="doSetVar"><l>promise</l><block s="reportNewList"><list><l>promise</l><block s="reportBoolean"><l><bool>false</bool></l></block><l></l></list></block></block><block s="fork"><block s="reifyScript"><script><block s="doReplaceInList"><l>3</l><block var="promise"/><block s="evaluate"><block var="computation"/><list></list></block></block><block s="doReplaceInList"><l>2</l><block var="promise"/><block s="reportBoolean"><l><bool>true</bool></l></block></block></script><list></list></block><list></list></block><block s="doReport"><block var="promise"/></block></script></block-definition><block-definition s="is %&apos;value&apos; a promise?" type="predicate" category="operators"><header></header><code></code><translations></translations><inputs><input type="%s"></input></inputs><script><block s="doReport"><block s="reportAnd"><block s="reportIsA"><block var="value"/><l><option>list</option></l></block><block s="reportAnd"><block s="reportEquals"><block s="reportListLength"><block var="value"/></block><l>3</l></block><block s="reportAnd"><block s="reportEquals"><block s="reportListItem"><l>1</l><block var="value"/></block><l>promise</l></block><block s="reportIsA"><block s="reportListItem"><l>2</l><block var="value"/></block><l><option>Boolean</option></l></block></block></block></block></block></script></block-definition><block-definition s="error %&apos;error&apos;" type="command" category="control"><header></header><code></code><translations></translations><inputs><input type="%txt"></input></inputs><script><block s="doRun"><block s="reportJSFunction"><list><l>error</l></list><l>throw new Error(error);</l></block><list><block var="error"/></list></block></script></block-definition><block-definition s="assert %&apos;condition&apos; else error %&apos;error&apos;" type="command" category="control"><header></header><code></code><translations></translations><inputs><input type="%b"></input><input type="%txt"></input></inputs><script><block s="doIf"><block s="reportNot"><block var="condition"/></block><script><custom-block s="error %txt"><block var="error"/></custom-block></script></block></script></block-definition><block-definition s="assert %&apos;promise&apos; is a promise" type="command" category="other"><header></header><code></code><translations></translations><inputs><input type="%l"></input></inputs><script><custom-block s="assert %b else error %txt"><custom-block s="is %s a promise?"><block var="promise"/></custom-block><l>expecting parallel computation but getting something else</l></custom-block></script></block-definition><block-definition s="%&apos;promise&apos; has completed?" type="predicate" category="other"><header></header><code></code><translations></translations><inputs><input type="%l"></input></inputs><script><custom-block s="assert %l is a promise"><block var="promise"/></custom-block><block s="doReport"><block s="reportListItem"><l>2</l><block var="promise"/></block></block></script></block-definition><block-definition s="wait for %&apos;promise&apos;" type="reporter" category="other"><header></header><code></code><translations></translations><inputs><input type="%l"></input></inputs><script><custom-block s="assert %l is a promise"><block var="promise"/></custom-block><block s="doWaitUntil"><custom-block s="%l has completed?"><block var="promise"/></custom-block></block><block s="doReport"><block s="reportListItem"><l>3</l><block var="promise"/></block></block></script></block-definition><block-definition s="after %&apos;promise&apos; do %&apos;computation&apos;" type="reporter" category="other"><header></header><code></code><translations></translations><inputs><input type="%l"></input><input type="%repRing"></input></inputs><script><custom-block s="assert %l is a promise"><block var="promise"/></custom-block><block s="doReport"><custom-block s="promise %repRing"><block s="reifyReporter"><autolambda><block s="evaluate"><block var="computation"/><list><custom-block s="wait for %l"><block var="promise"/></custom-block></list></block></autolambda><list></list></block></custom-block></block></script></block-definition><block-definition s="(promise) is %&apos;n&apos; prime or composite?" type="reporter" category="operators"><header></header><code></code><translations></translations><inputs><input type="%n"></input></inputs><script><block s="doReport"><custom-block s="promise %repRing"><block s="reifyScript"><script><block s="doFor"><l>i</l><l>2</l><block s="reportDifference"><block var="n"/><l>1</l></block><script><block s="doIf"><block s="reportEquals"><block s="reportModulus"><block s="reportQuotient"><block var="n"/><block var="i"/></block><l>1</l></block><l>0</l></block><script><block s="doReport"><l>composite</l></block></script></block></script></block><block s="doReport"><l>prime</l></block></script><list></list></block></custom-block></block></script></block-definition></blocks><variables><variable name="the promise"><l></l></variable><variable name="the number"><l>1187</l></variable></variables></project><media name="Promises" app="Snap! 5.4, http://snap.berkeley.edu" version="1"></media></snapdata>