Skip to main content

Trying to call multiple snippets within a snippet. the child snippets have calls like save() or updateData() inside them. So you could see how calls from the first snippet may actually finish after the second and so on.

I’m wondering if there is a way to wrap the getSnippet inside a $.when  .done and leverage deferred() promise multiple times in the parent snippet. Anyone have an example or thoughts on this?

If you have a parent snippet that calls multiple asynchronous functions or other snippets within it, then I’d absolutely advise you to leverage Promises to ensure that your logic is appropriately waiting for asynchronous code to finish before moving on.

One key thing to know is that “save()” and “updateData()” already return Promises, so if you have a snippet that ends with a call to either of these, then you can just return the result of the save() or updateData() call and it will be a Promise. 

Also, these methods can be chained using .then(), for instance


<br>// In a snippet...<br>return myModel.save()<br>.then(function(){<br>&nbsp; &nbsp;return myModel.updateData();<br>})<br>.then(function(){<br>&nbsp; &nbsp;return anotherModel.save();<br>})<br>.then(function(){<br>&nbsp; &nbsp;return anotherModel.updateData();<br>});

Hey Zach, thanks so much for helping me. Still trying to get this to work right. Am pretty close but the snippet on the return is still giving me some trouble. Snippet has a $.Deferred() & returns a promise but it continues onto the next call before the promise is resolved. is there something I’m missing here?

how I’m calling the snippet in the then


&#46;then(function(){<br />&nbsp;&nbsp;&nbsp;&nbsp;console&#46;log('retrieve otm then');<br />&nbsp;&nbsp;&nbsp;&nbsp;return skuid&#46;snippet&#46;getSnippet('retrieveOTMD')();<br />&nbsp;&nbsp;&nbsp;})

Reply