Have db calls perform asynchronously

I want to run multiple queries or updates/inserts one after another. Currently all of them are launched at the same time.
Is there a way (besides nesting them) to have them run asynchronously?

Hi Aaron,

Since each wix data operation returns a promise, and promises are chainable, you can run them one after the other without nesting. Here’s an example with and without nesting:

Nested promises:

wixData.get('collectionName', 'id1')
	.then(() => 
		wixData.get('collectionName', 'id2')
			.then(() => 
				wixData.get('collectionName', 'id3')
					.then(wixData.get('collectionName', 'id4'))))

Chained promises:

wixData.get('collectionName', 'id1')
	.then(() => wixData.get('collectionName', 'id2'))
	.then(() => wixData.get('collectionName', 'id3'))
	.then(() => wixData.get('collectionName', 'id4'))

You can read more about promises here: JavaScript Promises: an introduction

In the future we will support the async-await syntax which will make using asynchronous code much simpler.

ok so then if i were to add code based on each of those query results like

wixData.get(‘collectionName’, ‘id1’)
.then((results) =>{
let queryResults1 = results.items;
})
.then(() => wixData.get(‘collectionName’, ‘id2’))
.then((results) =>{
let queryResults2 = results.items;
})

i can assume since they are being chained that the second query wouldnt run until the first results logic is completed correct?

If so then this does help me out. Im impressed at the detail and control Wix Code is allowing so far.
Thanks for helping clear this up.

Yes, that’s correct. Thanks for the kind words and have fun coding!

Hey Tomer hopefully you see this.
I tried out your suggestion but i noticed that i would end up using both nested and chaining since i have to run queries based on results from a previous query.
Because of this i noticed that when it gets to the next .then statment after the nested query the code starts to run the next .then statement asynchronously. Is there truly no way to stop this or in my case (doing query on previous results causing the need to be nested).

I’ve also noticed that im having difficulty handling promise rejection, i’ve tried adding catch or onRejected logic to my .then statements but it seems like it does not completely stop the chain.

Ex Code:
let firstRecord;
wixData.query(“somCollection”).find()
.then((results) => { //result of the first query
firstRecord = results.items[0];
})
.then(() => { //second then this requires first queries result
wixData.query(“otherCollection”).eq(“points”, firstRecord.points).find()
.then((pointsResult) => { //nested query because need to do logic based off firstRecord
//do logic
})
})
.then(() => { //third then statement, this one launches right after the query in the second then statement finishes but asynchronously with the above then statement. this makes it possible for logic to fail if it needs variables set in the above then statement
//do logic
})

Hi Aaron,

Another note about promises - the value you return from one promise is passed to the next callback on the chain. I modified your code to demonstrate this. I also added a catch callback that will catch any error in the entire chain:

wixData.query("somCollection").find()
.then((results) => {
   // the following line will pass the value to the next callback
   return results.items[0];
})
.then((firstRecord) => {
   return wixData.query("otherCollection").eq("points", firstRecord.points).find()
})
.then((pointsResult) => {
	// do something with pointsResult
})
.catch((error) => {
	console.log('something went wrong:', error);
})

BUT - Assuming you’re not doing any async logic in the first callback, you can just merge the first and second callbacks:

wixData.query("somCollection").find()
.then((results) => {
   const firstRecord = results.items[0];
   return wixData.query("otherCollection").eq("points", firstRecord.points).find()
})
.then((pointsResult) => {
	// do something with pointsResult
})
.catch((error) => {
	console.log('something went wrong:', error);
})

I hope this helps.

thanks for the quick response that makes more sense ill have to try this out!