最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

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

matteradmin4PV0评论

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.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far