what is the easiest way to extract data from the Promise<any>? #93
-
How do I extract data in a easy way? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
1. How do I extract Json keys in a safe way from the returned Promise?First, you have to wait for Promise then/catchconst promise = new Promise<any>((resolve, reject) => resolve({
key1: 'val1',
key2: 'val2',
key3: 'val4',
}));
promise.then((data) => {
...
}); Async/Awaitconst promise = new Promise<any>((resolve, reject) => resolve({
key1: 'val1',
key2: 'val2',
key3: 'val4',
}));
async function main() {
const data = await promise;
}
main(); The next step is to use the built-in function in Object.keys(data); Final result: Promise then/catchconst promise = new Promise<any>((resolve, reject) => resolve({
key1: 'val1',
key2: 'val2',
key3: 'val4',
}));
promise.then((data) => {
console.log(Object.keys(data));
}); Async/Awaitconst promise = new Promise<any>((resolve, reject) => resolve({
key1: 'val1',
key2: 'val2',
key3: 'val4',
}));
async function main() {
const data = await promise;
console.log(Object.keys(data));
}
main(); 2. Trying to get all subtask information from all the existing subtasks. (Currently only my own) It seems like I get a lot of unnessessary data. Do you know of a query that retrieves the user inserted data only? Would you recommend another query?I don't think you can avoid getting unnecessary information. Do you really need to reduce your Internet traffic as much as possible? I think that your method of obtaining subtasks is suitable for use and there is no need to change anything. 3. I seem to always get Promise back even when trying resolve, .json(), const var = () => { } eg. https://flaviocopes.com/javascript-promises/#consuming-a-promise What is the simplest way?You cannot immediately work with data wrapped in Promise. First get the data from Promise, and then you can work with it. Jira.js automatically converts data from plain text to json, you don't need to do anything else. |
Beta Was this translation helpful? Give feedback.
1. How do I extract Json keys in a safe way from the returned Promise?
First, you have to wait for
Promise<any>
to execute, which you can do in two ways:Promise then/catch
Async/Await
The next step is to use the built-in function in
JavaScript
:Final result:
Promise then/catch