Evandro Amaral
Evandro Amaral

Reputation: 1

Ionic / Angular - Transform Promise in callback - Storage don't get a value to use in next functions

I have a token in storage memory, but on load with "get" method:

import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage-angular';

@Injectable({
  providedIn: 'root'
})
export class StorageService {

  constructor(private storage: Storage) {
    this.initStorage();
   }

  async initStorage() {
    await this.storage.create();
  }

  async get(key) {
    const value = await this.storage.get(key); 
    //console.log(value);
    return value;
  }

I have a "promise", and my call to API don't fill the correct value:

public getCategories () {
let wsfunction = "core_course_get_categories";
this.url = this.variaveis.URL_REST + "wsfunction=" + wsfunction + "&wstoken=" + this.storage.get('token');
console.log(this.url);
return this.http.get(this.url);
}

The final URL: https://...com/webservice/rest/server.php?moodlewsrestformat=json&wsfunction=core_course_get_categories&wstoken=[object Promise]

My next function use the HTTP return from API do load data:

...
this.courses.getCategories().subscribe({
next: (data) => {
console.log(data);
...

But fail because don't get values but a "object promise".

How to convert the result of "get" Storage in values, and don't in 'Promise Object'??

Any function to transform the values in promise to values in Callback format? to use in next functions in my code? because I don't want to rewrite the final functions because there is a lot of code depending on them, I wanted to solve it at the beginning of "get" storage...

I don't have problem with performance, my app don't have many interactions and don't load many amount of data.

Upvotes: 0

Views: 16

Answers (0)

Related Questions