Browse Source

fix async bug

Carsten Porth 6 years ago
parent
commit
f4f95b9ff5

+ 1 - 0
app/src/pages/home/home.ts

@@ -34,6 +34,7 @@ export class HomePage {
   doRefresh(refresher) {
     this.twitter.fetchHomeFeed().then(res => {
       this.data = res.data;
+      console.log(res);
       refresher.complete();
     });
   }

+ 1 - 1
app/src/pages/profile/profile.ts

@@ -66,7 +66,7 @@ export class ProfilePage {
   }
 
   private async checkP2pTweets(userId) {
-    const lastTweetHash = this.gun.getLastTweetFromUser(userId);
+    const lastTweetHash = await this.gun.getLastTweetFromUser(userId);
     const res = await this.ipfs.fetchTweet(lastTweetHash);
   }
 }

+ 0 - 1
app/src/pages/write-tweet/write-tweet.ts

@@ -8,7 +8,6 @@ import {
 import { FormBuilder, Validators, FormGroup } from "@angular/forms";
 import { TwitterApiProvider } from "../../providers/twitter-api/twitter-api";
 import { Storage } from "@ionic/storage";
-import Gun from "gun";
 import { P2pStorageIpfsProvider } from "../../providers/p2p-storage-ipfs/p2p-storage-ipfs";
 import { P2pDatabaseGunProvider } from "../../providers/p2p-database-gun/p2p-database-gun";
 

+ 7 - 8
app/src/providers/p2p-database-gun/p2p-database-gun.ts

@@ -19,13 +19,12 @@ export class P2pDatabaseGunProvider {
     this.gun.get(userid).put({ lastTweet: hash });
   }
 
-  public getLastTweetFromUser(userid): string {
-    let hash;
-    this.gun
-      .get(userid)
-      .get("lastTweet")
-      .once(lastTweet => (hash = lastTweet));
-
-    return hash;
+  public async getLastTweetFromUser(userid) {
+    return new Promise(resolve =>
+      this.gun
+        .get(userid)
+        .get("lastTweet")
+        .once(resolve)
+    );
   }
 }