# WCSTerrainProvider and IndexedDB : How to chain two promesis ?

**URL:** https://community.cesium.com/t/wcsterrainprovider-and-indexeddb-how-to-chain-two-promesis/1833
**Category:** CesiumJS
**Created:** [January 6, 2015, 10:43am UTC](https://community.cesium.com/t/wcsterrainprovider-and-indexeddb-how-to-chain-two-promesis/1833 "2015-01-06T10:43:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![xavier\_lhomme](https://avatars.discourse-cdn.com/v4/letter/x/e5b9ba/32.png) [@xavier\_lhomme](https://community.cesium.com/u/xavier_lhomme)
#### Post date: [January 6, 2015, 10:43am UTC](https://community.cesium.com/t/wcsterrainprovider-and-indexeddb-how-to-chain-two-promesis/1833/1 "2015-01-06T10:43:43Z")

</div>

I added to my WCSTerrainProvider a mechanism allowing me to store locally the heighmap buffer with IndexedDB.  
The process works as follows:  
I search in IndexedDB if the requested tile is present locally.  
If the tile is present then I get the heighmap buffer and I return the HeightmapTerrainData  
But, if the tile is not stored locally, I submit a WCS request. If the query is executed correctly, I parse the returned geotiff and create the buffer that I store in the local database then I return the HeightmapTerrainData.

My first tests are promising.  
I encounter a problem to chain correctly the 2 promises in the requestTileGeometry function:  
I have to define a promise “request Tile In IndexedDB” and if this promise is rejected then I have to define a new promise “request Tile in WCS”

which gives:  
Cesium.when (requestTileInIndexedDB, function (tileData) {…  
return heightmapTerrainData ;  
}) .otherwise (Function (evt)  
{  
var promiseGetCoverage = Cesium.loadWithXh ({url: urlGetCoverage, responseType ‘ArrayBuffer’});  
Cesium.when (promiseGetCoverage, function (image) {…  
return heightmapTerrainData ; }) .otherwise …

})

What should return the first Otherwise function ? if I return the second promise Cesium throw an issue because I do not return a heightmapTerrainData but a promise.

xlhomme

---

<div class="post-metadata">

### Author: ![abdou](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/abdou/32/2012_2.png) [@abdou](https://community.cesium.com/u/abdou)
#### Post date: [January 6, 2015, 10:53am UTC](https://community.cesium.com/t/wcsterrainprovider-and-indexeddb-how-to-chain-two-promesis/1833/2 "2015-01-06T10:53:24Z")

</div>

Hello,  
you can see the documentation of when here : [https://github.com/cujojs/when/blob/master/docs/api.md#promisecatch](https://github.com/cujojs/when/blob/master/docs/api.md#promisecatch)

---

<div class="post-metadata">

### Author: ![Kevin\_Ring](https://sea2.discourse-cdn.com/cesium/user_avatar/community.cesium.com/kevin_ring/32/2003_2.png) [@Kevin\_Ring](https://community.cesium.com/u/Kevin_Ring)
#### Post date: [January 6, 2015, 11:12am UTC](https://community.cesium.com/t/wcsterrainprovider-and-indexeddb-how-to-chain-two-promesis/1833/3 "2015-01-06T11:12:47Z")

</div>

Hi,

Returning the chained promise from the otherwise callback should work. If you’re still having trouble getting that to work, please share your exact code and the error that you’re seeing.

Kevin

---

<div class="post-metadata">

### Author: ![xavier\_lhomme](https://avatars.discourse-cdn.com/v4/letter/x/e5b9ba/32.png) [@xavier\_lhomme](https://community.cesium.com/u/xavier_lhomme)
#### Post date: [January 6, 2015, 2:31pm UTC](https://community.cesium.com/t/wcsterrainprovider-and-indexeddb-how-to-chain-two-promesis/1833/4 "2015-01-06T14:31:03Z")

</div>

Indeed it works ! I wasn’t returning the promesis but undefined…
