Getting Data from A Database Based on fieldkey

Hi there,

To access the returned data from a promise, you can either store the value in a higher scoop variable, or you can store the value directly in a variable, I’ll explain both methods.

// The actual promise we need to get the data from
const getData = () => {
    return new Promise(resolve => {
        setTimeout(resolve('Ahmad'), 2e3)
    })
}

Method #1: Store the result in a higher scoop variable.

let answer; // Currently, it's: undefined

// Invoke the function:
await getData().then(x => answer = x);

console.log(answer); // Prints: Ahmad

Method #2: Store the value in a variable, directly (The most common way).

const answer = await getData();

console.log(answer); // Prints: Ahmad

Note: Whenever you use the await keyword, it needs to be inside an async function. Learn more.

Hope this helps~!
Ahmad