簡體   English   中英

與react-native一起使用await

[英]Use await with react-native

我想異步調用一個函數。 我在MyClass中編寫了函數代碼

static currentPassword(){
    OtherClass.getPassword().then((data) => {
      alert(JSON.stringify(data)); // this works fine
      return 'test';  // will return data.password at the end
      });
  }

在同一個類中,我正在創建此函數

static async requestUser(){
      const token = await MyClass.currentPassword(); // this call the function successfully 
      alert(token); // this print 'undifined'
}

在requestUser中,調用currentPassword() ,但返回值未定義

您必須返回返回“測試”的承諾。

static currentPassword(){
  return OtherClass.getPassword().then((data) => {
    alert(JSON.stringify(data));
    return 'test';
  });
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM