Skip to content

what is the easiest way to extract data from the Promise<any>? #93

Answered by MrRefactoring
Joseaestevez asked this question in Q&A
Discussion options

You must be logged in to vote

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

const promise = new Promise<any>((resolve, reject) => resolve({
  key1: 'val1',
  key2: 'val2',
  key3: 'val4',
}));

promise.then((data) => {
  ...
});

Async/Await

const 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 JavaScript:

Object.keys(data);

Final result:

Promise then/catch

const promise = new Promise<any>((resolve, reject) => resolve({
  

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@th3f0r3ign3r
Comment options

@th3f0r3ign3r
Comment options

Answer selected by MrRefactoring
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants