What's the best way to use Javascript Promises

Hi, i’m working with my daughter on a applab application.
All of the database access means lots of callbacks.
So, i’m using a minimal Javascript Promises library.
I cut and paste it into my code.
Is there a better way to do this? It’s not a function so i’m not sure how to include it in my project.
The applab javascript interpreter version does not support Promises.
Javascript promises confuse me. I’m not sure how i can teach them to my daughter. Are there styles of using applab which deal with database access better.

Hi @peterrham,

Welcome to the code.org forum! Awesome that you’re working with your daughter on an app. :slight_smile:

I’m not familiar with Javascript Promises, but it may help you set your expectations to know that AppLab isn’t meant to be a fully-featured programming environment. Instead, the features and support are prioritized to support the CS Principles curriculum. It also happens to be able to do much more than that - but I generally wouldn’t expect it to.

In this case, it’s possible that the demands of your daughter’s app are hitting the limitations of AppLab.

It’s always great to let us know what features you’d like to see more of and depending on demand and resources, it might make it to AppLab. Feel free to keep posting on this forum (especially to get more community response), and/or give direct input by emailing support@code.org.


App Lab uses the ES5 version of Javascript, some features are omitted on purpose, and some of it doesn’t work properly. ES5 was released in 2009 so there is a lot of things missing.

You don’t need Promise in App Lab. You don’t have access to the DOM nor the global context so there isn’t a way to get into much trouble. If you really do need that kind of functionality App Lab does have try catch finally. By the way, you can use that to create a unit testing framework.

So the negative side to using ES5 is that it doesn’t have all the fixes from the last 11 years of the language being used hard. On the plus side, you can get ES5 books for free to download.

In terms of teaching your daughter to code, I would only use named functions as callbacks. That whole function expression overuse caused by block mode needs correction. Here is some code that handles database connection delays using native App Lab functionality. https://studio.code.org/projects/applab/pB0dvQx_7hl7_MhsgaQKN6CCXKfYRJK01KBSVVQ_sNk It also includes a unit testing framework and some unit tests. Test first development is something I would encourage. Feel free to copy paste.

Thanks so much for your response and the nicely written sample code!

I looked through your sample and i do not see querying of two different tables in series. I know that there are ways of doing this without promises, of course, but i was looking for the most succinct way. Am i missing something?

My daughter’s app is a chat application with users and messages which i keep in separate tables.

I will ponder your response. My code uses promises, but i’m not sure what is easier to explain to her, asynchronous call backs or Promises.

She thinks that her project will need to graduate to so something with more functionality than applab. I like app lab quite a lot and it has been easy (and elegant) to do her project so far and I hate too lose that. What would you suggest for the next step beyond app lab for kids to write and deploy web applications? i’m thinking of hosting a single page web app hosted in AWS S3. I’m trying to think what would have the least moving parts for her (and me!).

Thanks again!

The way I mitigated the complexity of database callbacks is to grab all the contents in advance and just work from the array of JSON objects. When a JSON object is changed or created it is marked changed or new. I then add or update those changes incrementally keeping the database and the array in sync. The database code becomes very simple with about 24 lines. I don’t worry much about the callbacks for updating and creating records because those calls either succeed or hang forever anyway.

A Promise isn’t going to help you here, try catch doesn’t even help because it is not an error in App Lab for a database operation to hang forever.

Depending on what you are trying to do there is a new function getColumn which doesn’t require a callback. You only get one column, but it might be what you need in many cases.

Thanks! I was thinking of the use case where i query more than one table up front where the results of one depend upon the results of the following one.
Anyway, the lightweight promise library



Including the promise library at the top of the program is a bit confusing, but otherwise, it works pretty well.