Better way to get multiple values out of a promise? (getmember)

Working with getmember() and wondering if there is a better way to capture multiple values to variables using a promise? I’m able to get each value individually but struggling to believe this is the best use/way to do this, thoughts?

import { currentMember } from ‘wix-members-frontend’;

var FirstName
var LastName
var DOB
var BirthCountry
var CurrentCity

$w.onReady(async function () {

FirstName = await currentMember.getMember()
    .then((member) => {
        let FirstName = member.contactDetails.firstName;
        return FirstName;
    })
    .catch((error) => {
        console.error(error);
    });

LastName = await currentMember.getMember()
    .then((member) => {
        let LastName = member.contactDetails.lastName;
        return LastName;
    })
    .catch((error) => {
        console.error(error);
    });

DOB = await currentMember.getMember()
    .then((member) => {
        let DOB = member.contactDetails.birthdate;
        return DOB;
    })
    .catch((error) => {
        console.error(error);
    });

BirthCountry = await currentMember.getMember()
    .then((member) => {
        let BirthCountry = member.contactDetails.addresses[0].country;
        return BirthCountry;
    })
    .catch((error) => {
        console.error(error);
    });

CurrentCity = await currentMember.getMember()
    .then((member) => {
        let CurrentCity = member.contactDetails.addresses[1].city;
        return CurrentCity;
    })
    .catch((error) => {
        console.error(error);
    });

console.log(FirstName)
console.log(LastName)
console.log(DOB)
console.log(CurrentCity)

console.log(BirthCountry)
});

You are correct. and a Single promise returns all the data and it will complete the task faster than multiple promises.

const member=await currentMember.getMember();
const BirthCountry= member.contactDetails.addresses[0].country;
const CurrentCity = member.contactDetails.addresses[1].city;
const DOB = member.contactDetails.birthdate;
2 Likes

Thanks for the quick reply. Using your input I still couldn’t get it to work, can you assist?

$w.onReady(async function () {

const member = await currentMember.getMember();
let FirstName = member.contactDetails.firstName;
let LastName = member.contactDetails.lastName;
const BirthCountry = member.contactDetails.addresses[0].country;
const CurrentCity = member.contactDetails.addresses[1].city;
const DOB = member.contactDetails.birthdate;

console.log(FirstName)
console.log(LastName)
console.log(DOB)
console.log(CurrentCity)
console.log(BirthCountry)

});

Ok let’s check…

  1. Why you are doing all your request on frontend, instead doing them all on backend → bundle all responds into an object on backend and sending them back once to frontend?

On backend everything works faster.

  1. Already thought about to use promiseAll() ???

Read all those articles about PROMISES…
a) Using WixDataQuery.hasSome versus Promise.all
b ) Promises, Promises
c) Advanced promise chain not working
d) Promise.all not working in backend code - #2 by givemeawhisky

…try to learn from it.

Good luck!