I know similar questions to this have been asked before but I have reviewed them and still am unable to apply to my specific use case. I would greatly appreciate any help that anyone is able to provide.
A bit of background, I have a search tool that users can input a postcode into. Once they click 'Search' the postcode is converted into a Geolocation.
Here is a link to a codepen with the relevant Javascript
const postcodeToBeConverted = "BB80LD";
const conversionTable = {
BB80LD: [-2.1702448, 53.85482989]
};
// Simplified version of MongoDB query
const postcodeToGeo = new Promise(resolve => {
return resolve(lookup.BB80LD);
});
// 1.1 Renders incorrectly
// console.log(postcodeToGeo.then(res => res));
// 1.2 Renders correctly
// postcodeToGeo.then(res => console.log(res));
// Injecting value into another object
const masterQuery = {};
// How can inject the Geo value rendered by function 1.2 ?
masterQuery.postcode = postcodeToGeo.then(res => res);
/*
* Desired output =>
* masterQuery: {
* postcode: [-2.1702448,53.85482989]
* }
*
* Actual output =>
* masterQuery: {
* postcode: [object Promise]
* }
*/
console.log(masterQuery);
My attempts so far:
/* I can't update the value of a variable:
*
* let postcodeholder;
* postcodeToGeo.then(res => {postcodeholder = res});
* console.log(postcodeholder);
*/ Output => undefined
/* I can't store the return value in a variable:
*
* const postcodeholder = postcodeToGeo.then(res => res);
* console.log(postcodeholder);
*/ Output => [object Promise] {}
/* I can't pass the value as part of a function:
*
* const returnObj = async () => {
* postcodeToGeo.then(res => {
* return {
* masterQuery: {
* postcode: res
* }
* };
* });
* };
* returnObj();
*
* Output => Object {
* postcode: [object Promise] {}
* }
Thank you in advance.