Comments (2)
这么简单的场景,我觉得应该是你的代码逻辑问题。
这个库每个操作都是异步的,你最好把每一步用 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.
这么简单的场景,我觉得应该是你的代码逻辑问题。 这个库每个操作都是异步的,你最好把每一步用 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)
- 怎么获取到最新的值 HOT 10
- syncInBackground: false seems to not work and always leave data undefined HOT 4
- Request - Publish 1.0.1 HOT 2
- 如何同步获取数据? HOT 4
- RN warning when calling getAllDataForKey with many keys
- @react-native-community/async-storage could not be found within the project. HOT 3
- Will I lost data on expire time change? HOT 2
- Please do not use "_" in key!"data" is required in save()!
- "data" is required in save() HOT 3
- 怎么才能取出异步存的值,然后把它给header呢 HOT 18
- ERROR: The modules ['@react-native-community_async-storage', 'react-native-community_async-storage'] point to the same directory in the file system. Each module must have a unique path. HOT 2
- How can I get list of all keys?
- Bundle size doubled in 1.0.1? HOT 1
- database or disk is full HOT 1
- Vulnerabilities during npm install
- Update peer dependencies
- Unhandled JS Exception: [@RNC/AsyncStorage]: NativeModule: AsyncStorage is null. HOT 1
- How to Ignore expiry HOT 6
- I have a issue when i run on iPhone HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from react-native-storage.