javascript - 如何将承诺对象存储在变量中
问题描述
我想知道如何将承诺作为对象存储在变量中
我的代码
读写函数
async function storage(data, key) {
await AsyncStorage.setItem(`${key}`, JSON.stringify(data));
}
async function read(key) {
return new Promise((resolve, reject) => {
AsyncStorage.getItem(`${key}`, (err, data) => {
if (err) reject(err);
resolve(data);
});
});
}
我正在存储的对象以及我如何调用我的函数
let testing = {
swipe1: {
key: 1,
text: "Swipe1Text changed",
},
swipe2: {
key: "2",
text: "Swipe2Text",
},
};
storage(testing, "tasks@today");
// I'm able to console.log the following
let readStorageSwipes = read("tasks@today").then((result) => {
return result;
});
console.log(readStorageSwipes.swipe1);
如果需要一些参考,我正在使用以下库,这是一个 react native expo 项目
解决方案
假设你想存储读取承诺,因为你在读取函数中返回一个承诺,你可以直接存储承诺
readPromise = read("tasks@today");
readPromise
变量将是一个承诺对象
推荐阅读
- html - 为什么从 div 中删除文本会导致此布局中断
- json - 在 Vue 中使用外部 JSON 文件
- jenkins - 如何从晚上 8 点到早上 6 点在 Jenkins 中安排构建作业?
- kdb - 根据列值重塑表格
- google-cloud-automl - 上传到 Google Cloud Functions 时 AutoMl 预测是否不起作用
- java - 这两个代码有什么区别吗?
- python - 熊猫派生列中的对象到时间转换
- amazon-web-services - 如何将方法请求正文中的值映射到 API 网关映射模板?
- vb.net - VB.NET 从 0 到 4 舍入 --> 0 从 5 到 9 --> 1
- python - 如何在 Python 中将输入字符串更改为对象的名称