1

I want to know how I can store a promise as an object inside a variable

My Code


Read and Write Functions

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);
        });
    });
}

My object which is being stored and how I'm calling my functions

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);

I'm using the following library if some reference is required, this is for a react native expo project

rishi
  • 652
  • 1
  • 6
  • 20
  • You're storing the promise just fine in the `readStorageSwipes` variable? – Bergi Nov 27 '20 at 20:15
  • [Drop the pointless `.then((result) => { return reslt; })`](https://stackoverflow.com/q/41089122/1048572), though. – Bergi Nov 27 '20 at 20:16
  • @Bergi Sorry I should have been clear, I want to store the promise as an object inside the variable – rishi Nov 27 '20 at 20:17
  • You *are* storing the promise, which is an object with `.then` and `.catch` and `finally` methods, in the variable. Do you mean you want to store the result of the promise, the object that it fulfills with, in the variable? Then you will need to wait for the promise, with `await` or `.then()`. – Bergi Nov 27 '20 at 20:26
  • @Bergi Yes, but isn't that what I'm doing? – rishi Nov 27 '20 at 20:43
  • You mean waiting for the result? No, the `.then()` callback waits for the result and will be called when the promise is fulfilled, but the return value of the `.then()` call is another promise and *that*'s what you're storing in the `readStorageSwipes` variable. – Bergi Nov 27 '20 at 21:13
  • @Bergi Can you write an answer describing how I can store the object returned by the promise in a variable – rishi Nov 27 '20 at 21:26
  • You already have such a variable, named `result`, available in the callback function. In general, it's a bad idea to try storing the result object in an outer-scope variable. What do you plan to use it for? – Bergi Nov 27 '20 at 21:42
  • @Bergi that object contains the text and keys for a dynamic swipe able list. I wanted to store it inside a variable to make it easier to use everywhere. – rishi Nov 27 '20 at 22:04
  • Consider what to render while the object is still being loaded from the storage (not yet available). Since you're using react, you'll probably want to `useEffect` and `useState`. – Bergi Nov 27 '20 at 22:09

1 Answers1

0

Assuming you want to store read promise, since you're returning a promise in read function, you can directly store promise by

readPromise = read("tasks@today");

readPromise variable will be a promise object

Sante
  • 339
  • 3
  • 10
  • Sorry I should have been clear, I want to store that promise as an object inside a variable – rishi Nov 27 '20 at 20:16
  • perhaps you can explain what you're doing to do with the promise object. It is still not clear what you want to achieve. – Sante Dec 02 '20 at 19:06