
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to access object properties from result returned by async() function in JavaScript?
In this article, you will understand how to access object properties from result returned by async() functions in JavaScript. An object property in JavaScript is a variable that is associated with the object itself, i.e. the properties have a name and value is one of the attributes linked with the property.
Example 1
In this example, let’s understand how to access objects property using dot notation
console.log("A function is created that returns promise object") const promiseFunction = (input) => { return new Promise((resolve, reject) => { return resolve({ val: input }) }) } console.log("
Calling the function using dot notation") async function test() { const result = await promiseFunction("This is an asynchronous function response") console.log(result.val); } test();
Explanation
Step 1 − Define a function ‘promiseFunction’ that returns a promise.
Step 2 − Define an async function ‘test’ that accesses the property of the object using dot notation.
Step 3 − Display the result.
Example 2
In this example,
console.log("A function is created that returns promise object") const promiseFunction = (input) => { return new Promise((resolve, reject) => { return resolve({ val: input }) }) } console.log("
Calling the function using bracket notation") async function test() { const result = await promiseFunction("This is an asynchronous function response") console.log(result["val"]) } test();
Explanation
Step 1 − Define a function ‘promiseFunction’ that returns a promise.
Step 2 − Define an async function ‘test’ that accesses the property of the object using bracket notation.
Step 3 − Display the result.
- Related Articles
- How to add, access, delete, JavaScript object properties?
- How to create an object and access its properties in JavaScript?
- How to access JavaScript properties?
- Accessing an array returned by a function in JavaScript
- Shortest syntax to assign properties from function call to an existing object in JavaScript
- How to access variables declared in a function, from another function using JavaScript?
- Extract properties from an object in JavaScript
- How to duplicate Javascript object properties in another object?
- How to create object properties in JavaScript?
- How to delete object properties in JavaScript?
- How to use await outside of an async function in JavaScript?
- How to access an object through another object in JavaScript?
- How to throw an error in an async generator function in JavaScript ?
- How can we divide the result set returned by MySQL into groups?
- How to add, access JavaScript object methods?
