0

probably a bit of a silly question but I'm only a beginner and there's something I am trying to understand properly.

When we have code like this:

const { userName } = await getUserProfile({ userId });

What exactly is happening here? Are we destructuring userName out of the getUserProfile object to be able to access that property? And does userId mean that the getUserProfile function has a parameter that is an object that has a property userId?

Sorry, if this is too beginner of a question but would really appreciate it if anyone had the time to explain this to me please.

5
  • 1
    getUserProfile is a function which returns a promise. The ({ userId }) part is creating an object with the property shorthand. The promise is being awaited and then userName is being assigned/destructured from the returned value. Commented Dec 9, 2021 at 14:56
  • Just translate this code in mind to this one: const user = await getUserProfile({ userId }); const userName = user.userName Commented Dec 9, 2021 at 14:56
  • You are correct on both assumptions. Commented Dec 9, 2021 at 14:56
  • Thank you very much everyone. It was really bothering me not fully understanding what was going on here. Commented Dec 9, 2021 at 14:59
  • developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Dec 9, 2021 at 15:03

1 Answer 1

1

I'd say the function does something like this:

interface Options {
   userId: number;
}

interface ReturnItem {
  userName: string;
}

const getUserProfile = async (options: Options): Promise<ReturnItem> => {
  // make some request with the id
  const response = await axios.get(`/look/up/my/user/${options.userId}`);
  return response.data;
}

The destructuring is based off of the types defined in the function, so instead of getting an object back, you're just pulling the item out of the returned object as the types define that's what you can expect to receive.

Instead of doing the following:

const myId = 1;
const myResult = await getUserProfile({userId: myId});
console.log(myResult.userName);

you can do:

const userId = 1;
const {userName} = await getUserProfile({userId});
console.log(userName);

Another example:

const myReturnData = {
  userName: 'Bjork'
};

userName can now be accessed in the following ways:

console.log(myReturnData.userName);

or

const {userName} = myReturnData;
console.log(userName);

Destructuring is taking those values in your object and assigning them to variables.

A bit further..

const myReturnData = {
  userName: 'Bjork',
  coolnessFactor: 10000
};

const {userName, coolnessFactor} = myReturnData;
console.log('My User is: ', userName);
console.log('and my Coolnessfactor is: ', coolnessFactor);
Sign up to request clarification or add additional context in comments.

6 Comments

Curious, why have you answered with typescript example? It's neither tagged, nor mentioned in the question.
ha! Fair point @evolutionxbox :) Happy to remove them. Types help explicitly show what's going on if there are other assumptions being made. I'll happily remove them if they cause confusion.
Don't remove them on my account. It's just whether they'll help or hinder, since types in TS are more specific than JS.
Thanks for the reply! Appreciate it. This part "so instead of getting an object back, you're just pulling the item out of the returned object as the types define that's what you can expect to receive." helped explain this to me.
Completely agree. One of the things with destructuring is knowing what's returned. My hope was by showing an example with defined types it would be easier to understand why you could destructure on the userName. Again, happy to annotate the answer with that as I'd hate to cause additional confusion! Thank you for pointing that out.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.