Ionic 3 - Storage

Hi folks,

is there a better way to prove, that a key<->value pair was stored successfully into the storage / could be fetched successfully from the storage?

Ionic Storage: https://ionicframework.com/docs/storage/

storage set

this.storage.set('key01', 'value01').then(() => {
	// key01 with value01 was stored successfully into the local storage, continue with code execution

	this.storage.set('key02', 'value02').then(() => {
		// key02 with value02 was stored successfully into the local storage, continue with code execution, and so on ...
	});
});

##storage get

this.storage.get('key01').then(() => {
	// key01 has value01 continue with code execution

	this.storage.set('key02', 'value02').then(() => {
		// key02 has value02 continue with code execution, and so on ...
	});
});

If I - say for example - save five key<->value pairs into the storage, so the chain would be really big and hard to read. That’s the point of the question.

Best,
Unkn0wn0x

Two options come to mind, but only one is probably viable for retrieving.

Promise.all()

storeStuff(): Promise<void> {
  return Promise.all([
    this.storage.set('k1', 'v1'),
    this.storage.set('k2', 'v2'),
  ]);
}

fetchStuff(): Promise<string[]> [
  return Promise.all([
    this.storage.get('k1'),
    this.storage.get('k2'),
  ]);
}

chaining

This isn’t as ugly as you think, because you can return a Promise out of a then clause. Probably only best for storing.

storeStuff(): Promise<void> {
  return this.storage.set('k1', 'v1')
    .then(() => this.storage.set('k2', 'v2'));
}

Thanks, that looks good, I will try it out.