typescript - IonicAngular - Transform Promise in callback - Storage don't get a value to use in next functions - Stack O

I have a token in storage memory, but on load with "get" method:import { Injectable } from &

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://../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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745591301a4634848.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信