How to return a value from a async function in array.map in javascript?

chaitanya ivvala

how to return the value from an async function in the array.map I am trying to access the value from them but I am getting Array [[object Promise], [object Promise], [object Promise], [object Promise]]?

I want async inside the array1.map because I have some process that takes time so I need to use await.

var array1 = [1, 4, 9, 16];

// pass a function to map
const test = async ()=>{
const map1 = await array1.map(async x => 'hi');

return map1;
// expected output: Array [2, 8, 18, 32]
}
test().then((data)=>{
console.log(data)
})

expected output: Array ['hi', 'hi', 'hi', 'hi'] my output: [[object Promise], [object Promise], [object Promise], [object Promise]]

Alex

You need to use Promise.all function, where you put your array of promises

var array1 = [1, 4, 9, 16];

// pass a function to map
const test = async () => {
  const map1 = await Promise.all(array1.map(async x => 'hi'));
  return map1;
}

test().then((data) => {
  console.log(data)
});

expected output: Array ['hi', 'hi', 'hi', 'hi']

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to return value from debounced function in javascript?

分類Dev

How to return a value from a function , use that value to make a math formula and push the solution(key/value) to an array of objects?

分類Dev

How to correctly return value from function?

分類Dev

How to return value from a child process in a function?

分類Dev

How to get a return value from a connect function

分類Dev

How to return value from an extended casperjs function?

分類Dev

How I can return value from async block in swift

分類Dev

How to return value from JavaScript using Selenium?

分類Dev

Javascript async function return then-catch promise?

分類Dev

javascript return property value from nested array of objects based on condition

分類Dev

map function do not modifying the current array in javascript and return new array with modified

分類Dev

How can I print the return value of a JavaScript function in HTML?

分類Dev

Why does async array map return promises, instead of values

分類Dev

How to return one and only one value from a PowerShell function?

分類Dev

How to get the single return value from mysql stored function in php

分類Dev

How can I return a value from a parent function in Node?

分類Dev

PHP: how to return counter of value from recursive function?

分類Dev

How to properly return value from a data inside the function

分類Dev

How to get json return value from php by javaScript / jquery?

分類Dev

how to return an array from php to ajax response in javascript

分類Dev

How to get the data from a async function and used that as a property value for a json on another asyn function?

分類Dev

Return a promise on an external function with async data returned from an inner function

分類Dev

Lua: How to assigned a return value from a function to a local value of a different function in lua

分類Dev

How to return a failure inside .map function in a Result

分類Dev

how to pass a value from a recursive function into an array in a different method

分類Dev

How to return a list from a function?

分類Dev

How to return value from a Promise

分類Dev

How javascript function with 'return as function' works

分類Dev

Defining styles by value from a map function

Related 関連記事

  1. 1

    How to return value from debounced function in javascript?

  2. 2

    How to return a value from a function , use that value to make a math formula and push the solution(key/value) to an array of objects?

  3. 3

    How to correctly return value from function?

  4. 4

    How to return value from a child process in a function?

  5. 5

    How to get a return value from a connect function

  6. 6

    How to return value from an extended casperjs function?

  7. 7

    How I can return value from async block in swift

  8. 8

    How to return value from JavaScript using Selenium?

  9. 9

    Javascript async function return then-catch promise?

  10. 10

    javascript return property value from nested array of objects based on condition

  11. 11

    map function do not modifying the current array in javascript and return new array with modified

  12. 12

    How can I print the return value of a JavaScript function in HTML?

  13. 13

    Why does async array map return promises, instead of values

  14. 14

    How to return one and only one value from a PowerShell function?

  15. 15

    How to get the single return value from mysql stored function in php

  16. 16

    How can I return a value from a parent function in Node?

  17. 17

    PHP: how to return counter of value from recursive function?

  18. 18

    How to properly return value from a data inside the function

  19. 19

    How to get json return value from php by javaScript / jquery?

  20. 20

    how to return an array from php to ajax response in javascript

  21. 21

    How to get the data from a async function and used that as a property value for a json on another asyn function?

  22. 22

    Return a promise on an external function with async data returned from an inner function

  23. 23

    Lua: How to assigned a return value from a function to a local value of a different function in lua

  24. 24

    How to return a failure inside .map function in a Result

  25. 25

    how to pass a value from a recursive function into an array in a different method

  26. 26

    How to return a list from a function?

  27. 27

    How to return value from a Promise

  28. 28

    How javascript function with 'return as function' works

  29. 29

    Defining styles by value from a map function

ホットタグ

アーカイブ