Promises promises... or when to use async await

I came across this syntax by Thomas

And excuse me for not being too familiar with async and promises.
My question - can I still use async/await if I have multiple scene objects I want to find?

const obj0 = await Scene.root.findFirst(‘object00’);
const obj1 = await Scene.root.findFirst(‘object01’);

This seems wrong as the two operations should be possible to run in parallel right?

Really my question is simply: What is the most readable and efficient way to fetch multiple objects with async/await?

You’re right about wanting to run those in parallel (it’s faster). It can be done with the Promise.all() method, like this:

const sceneObjects = await Promise.all([

You’ll probably only notice a difference if you are fetching a super large number of scene objects, but it’s best practice to do it in parallel. I think Lasse did a speed test for this at some point.


Nice solution, thanx

Yes, like Josh said you can use Promise.all, but when testing some things I run a loop with await (for await...of) and it runs perfectly. The amount of objects you’d need to fetch to notice a difference is too big to really be a concern.

Also, that seems the perfect use case for findByPath, read more about it here


Yea findByPath is nice - the object names were just examples though.

Actually now that I am reminded of this I had a look and it’s right there in the template when I start a new script file - duh:
So basically:

const [obj0, obj1] = await Promise.all([
1 Like

Yes that’s alright.

Btw, the [ ] = ... is array destructuring which isn’t mentioned on the template even though it can confuse some people. Leaving a link here on the thread just in case


wow, I should read on MDN more. So many things you can do with destructuring.

1 Like

They are my go to for learning. They even have interactive consoles where you can test things* (only on some topics apparently). It’s amazing. Glad you found the link useful

* the console is for web so things like console.log obviously don’t work in Spark, but it’s still amazing

I’d also like to add this page in the docs:

This helped me a lot when I first saw the new script template. I’m still unclear on some thing, e.g. I put all of my script into the async in the template - I’m unclear on whether that is even necessary.
I also like the look of the .then()-block, but it appears to be the old way of doing things?

It’s only necessary if you want to use await. It’s just meant to make promises a bit easier to deal with because it can become a pain when there’s lots of async stuff going on (like on the web). In Spark, I don’t think it matters as much because generally the only async stuff happens when the effect initializes, so you can get away with just a single Promise.all()

So I could maybe just use the async bit to initialize/find scene objects and then leave that function and just work like pre-v85?

It’s up to you. There’s no need for async if you don’t need it. I usually do something like @Tomas_Pietravallo with array destructuring, but without await.

Promise.all([...]).then(([obj0, obj1, obj2]) => {...})

Cheers, looks great to me.