OiO.lk Blog javascript Pending promise stored as property in object array, do not know how to access to fulfill
javascript

Pending promise stored as property in object array, do not know how to access to fulfill


I’m trying to fill an array of objects with specific property values and am using Promises with async/await with Javascript in a Node.JS environment. Here is the main part:

getPublicNotes(connectHeaders, baseurl, pubKey, noteKeyList).then((nkl) => {
    if (nkl.length > 0) {
        getSecurityAbEntry(connectHeaders, baseurl, nkl);
        for (let count = 0; count < nkl.length; count++) {
            let secGroupKey = getSecurityAbEntry(connectHeaders, baseurl, nkl[count]);
            nkl[count].writeAccessKey = secGroupKey;
        }
    } else {
        console.log("EMPTY: There are no Public Notes; nothing to process.");
    }
    /* for (let count2 = 0; count2 < nkl.length; count2++) {
        let notesSecGroup = setNoteSecurity(connectHeaders, baseurl, nkl[count2]); 
        console.log(notesSecGroup);
    } */
});

noteKeyList is an array of objects, initially empty.
getPublicNotes(...noteKeyList) is an async function that returns an array of objects within a Promise.
getSecurityAbEntry(...nkl[count]) is an async function that takes in a single object and returns a single writeAccessKey value within a Promise. At the end of the for loop, the noteKeyList array looks like this:

[
{
abEntryKey: 'abc124der',
noteKey: '098ert'
writeAccessKey: Promise { <pending> }
},
{
abEntryKey: 'def321red',
noteKey: '392tkf'
writeAccessKey: Promise { <pending> }

},
{
abEntryKey: 'ghi645green',
noteKey: '9384fds'
writeAccessKey: Promise { <pending> }
},
...
]

How do I use then() to access each objects’ writeAccessKey property?

The setNoteSecurity() is what I want to use next on each object in the array but don’t know how to dig into each object in the array to feed into this async function.

Suggestions?

PS: I can give you the contents of each async function but I figure I’ll start with this in case it’s the functions’ contents is not necessary.



You need to sign in to view this answers

Exit mobile version