Giter Site home page Giter Site logo

Comments (2)

codering avatar codering commented on June 11, 2024 1

这么简单的场景,我觉得应该是你的代码逻辑问题。
这个库每个操作都是异步的,你最好把每一步用 Promise then 连起来(或者 async await), 才能确保是顺序执行的。

我提出的改进建议:

  • 让 setStorage 返回Promise
export function setStorage(keyId,data,i){
 ...
 ...
 return storage.save({
      key: key, 
      data: data,
      expires: i,
      id: id
    });
}
  • 改进你写的代码节选
if (succ && succ.code === '0000') {
    storage.remove({key:'token'}).then(() => {
           setStorage('token', succ.content.token, tokenTime).then(() => {
                 getTempInfo("token").then((token) => { 
                    //这里获取token
                 }
                 succallback();
          })
    })
} else {
    errcallback(succ.message);
}
// 你也可以通 async await 语法,可以看起来更简洁
  • 如果你没有用同步功能,最好把它关闭

from react-native-storage.

yesgit avatar yesgit commented on June 11, 2024

这么简单的场景,我觉得应该是你的代码逻辑问题。 这个库每个操作都是异步的,你最好把每一步用 Promise then 连起来(或者 async await), 才能确保是顺序执行的。

我提出的改进建议:

  • 让 setStorage 返回Promise
export function setStorage(keyId,data,i){
 ...
 ...
 return storage.save({
      key: key, 
      data: data,
      expires: i,
      id: id
    });
}
  • 改进你写的代码节选
if (succ && succ.code === '0000') {
    storage.remove({key:'token'}).then(() => {
           setStorage('token', succ.content.token, tokenTime).then(() => {
                 getTempInfo("token").then((token) => { 
                    //这里获取token
                 }
                 succallback();
          })
    })
} else {
    errcallback(succ.message);
}
// 你也可以通 async await 语法,可以看起来更简洁
  • 如果你没有用同步功能,最好把它关闭

非常感谢您的回复!!!
核心逻辑应该就是用then把异步变同步,我试一下,不知道大家都是这么写的吗?

from react-native-storage.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.