# Assigning value from getKeyValue to a variable

**URL:** <https://forum.code.org/t/assigning-value-from-getkeyvalue-to-a-variable/35251>\
**Category:** CS Principles\
**Created:** [October 11, 2021, 3:12am UTC](https://forum.code.org/t/assigning-value-from-getkeyvalue-to-a-variable/35251 "2021-10-11T03:12:03Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![redwgn67](https://avatars.discourse-cdn.com/v4/letter/r/41988e/32.png) [@redwgn67](https://forum.code.org/u/redwgn67)\
**Post date:** [October 11, 2021, 3:12am UTC](https://forum.code.org/t/assigning-value-from-getkeyvalue-to-a-variable/35251/1 "2021-10-11T03:12:03Z")

</div>

I’ve been trying to assign a value retrieved by the getKeyValue function to a variable that can be used outside of that funciton. However, nothing seems to work. Any suggestions?

---

<div class="post-metadata">

**Author:** ![jdonwells](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jdonwells/32/7900_2.png) [@jdonwells](https://forum.code.org/u/jdonwells)\
**Post date:** [October 11, 2021, 1:19pm UTC](https://forum.code.org/t/assigning-value-from-getkeyvalue-to-a-variable/35251/2 "2021-10-11T13:19:27Z")

</div>

Without the example code in front of me there is one thing that comes to mind. You don’t know when that variable will be assigned. There is no guarantee that the callback function will execute in a timely fashion.

---

<div class="post-metadata">

**Author:** ![bhatnagars](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/bhatnagars/32/4919_2.png) [@bhatnagars](https://forum.code.org/u/bhatnagars)\
**Post date:** [October 11, 2021, 3:30pm UTC](https://forum.code.org/t/assigning-value-from-getkeyvalue-to-a-variable/35251/3 "2021-10-11T15:30:39Z")

</div>

Please share a link to your code.

---

<div class="post-metadata">

**Author:** ![redwgn67](https://avatars.discourse-cdn.com/v4/letter/r/41988e/32.png) [@redwgn67](https://forum.code.org/u/redwgn67)\
**Post date:** [October 11, 2021, 4:33pm UTC](https://forum.code.org/t/assigning-value-from-getkeyvalue-to-a-variable/35251/4 "2021-10-11T16:33:18Z")

</div>

Based on the previous reply, I got it to work. A 1-s timeout for getKeyValue to complete made it work. Is there a better way to accomplish this?

> **[Check out what I made](https://studio.code.org/projects/applab/3urBiNBPmnmaEQjn6rzdWJhuMsR3MufBf4sJXzEyEPA)**
>
> I wrote the code myself with Code.org

---

<div class="post-metadata">

**Author:** ![jdonwells](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jdonwells/32/7900_2.png) [@jdonwells](https://forum.code.org/u/jdonwells)\
**Post date:** [October 15, 2021, 12:30am UTC](https://forum.code.org/t/assigning-value-from-getkeyvalue-to-a-variable/35251/5 "2021-10-15T00:30:48Z")

</div>

You need to create functions for each step in the process. Call those functions from the callbacks. Chain them together so they only get called when they are ready.

> **[Check out what I made](https://studio.code.org/projects/applab/Cp17LSBPE-OKVrlyD78WssxQ2Lgn9-u1uqVa5JPpg1I)**
>
> I wrote the code myself with Code.org

---

<div class="post-metadata">

**Author:** ![redwgn67](https://avatars.discourse-cdn.com/v4/letter/r/41988e/32.png) [@redwgn67](https://forum.code.org/u/redwgn67)\
**Post date:** [October 17, 2021, 10:44pm UTC](https://forum.code.org/t/assigning-value-from-getkeyvalue-to-a-variable/35251/6 "2021-10-17T22:44:16Z")

</div>

Thanks, that’s better and faster.
