如何从 Node.js 中的异步函数返回数组?
我们必须从另一个可以是异步或同步的函数中调用异步函数(我们可以传递数组或选择在异步函数本身中声明数组),然后从异步函数返回数组。
基本方法是包含一个 try-catch 块。如果在执行 try 块语句时发生任何错误,则使用 catch 块来处理异常。
示例 1:下面是我们调用print函数的代码。我们在这个函数中定义数组(在这种情况下是异步的),将它传递给另一个异步函数sort 。然后sort函数对数组进行排序并返回数组,然后我们从print函数中显示数组。
index.js
const sort = async (arr) => {
try {
let i, j, temp;
// Using bubble sort to sort the array
for (i = 0; i < arr.length; i++) {
for (j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
// The await prevents the
// execution of next line
// until this line is executed
temp = await arr[i];
arr[i] = await arr[j];
arr[j] = await temp;
}
}
}
// Returns the sorted array
return arr;
}
catch (e) {
// Display the error in case
// enything wrong happened
console.log(e)
}
}
const print = async () => {
// Use try-catch block to handle error
try {
// Define and initialize an array
let arr = [17, 90, 13, 10, 76]
// Call the "sort" function passing
// the array and store the result in
// "sortedArray", await will prevent
// the execution of next line until
// this line is executed.
const sortedArray = await sort(arr)
// Display sortedArray to check if
// everything works fine
console.log(sortedArray)
}
catch (e) {
// Display the error in case
// anything wrong happened
console.log(e)
}
}
// Calling print() is called
print();
index.js
const prime = async (N) => {
try {
const arr = []
// Iterate from 2 to N-1 to check
// which numbers are prime
for (var i = 2; i < N; i++) {
let j = 2
while (j < i) {
// Check if the number is
// divisble by any number
// except 1 and itself
if (i % j == 0) {
break
}
j++;
}
if (j == i) {
// If this condition holds
// true then it means that
// the number is not
// divisible by any number
// except 1 and itself.
// Therefore, it is a prime
// number and add this number
// to the array.
arr.push(j)
}
}
// Return the array
return arr
}
catch (e) {
// Display the error in case
// anything wrong happened
console.log(e)
}
}
const findPrime = async () => {
try {
let N = 20
// Call prime() passing argument
// N which is equal to 20 and
// store the result in an array
const primeAry = await prime(N)
// Print the array
console.log(primeAry)
}
catch (e) {
// Display the error in case
// anything wrong happened
console.log(e)
}
}
// Calling findPrime() method
findPrime()
使用以下命令运行index.js文件:
node index.js
输出:
[ 10, 13, 17, 76, 90 ]
示例 2:现在让我们看看从已在该异步函数中声明的异步函数返回数组的示例。该函数将返回一个包含所有小于给定数 N 的素数的数组。
index.js
const prime = async (N) => {
try {
const arr = []
// Iterate from 2 to N-1 to check
// which numbers are prime
for (var i = 2; i < N; i++) {
let j = 2
while (j < i) {
// Check if the number is
// divisble by any number
// except 1 and itself
if (i % j == 0) {
break
}
j++;
}
if (j == i) {
// If this condition holds
// true then it means that
// the number is not
// divisible by any number
// except 1 and itself.
// Therefore, it is a prime
// number and add this number
// to the array.
arr.push(j)
}
}
// Return the array
return arr
}
catch (e) {
// Display the error in case
// anything wrong happened
console.log(e)
}
}
const findPrime = async () => {
try {
let N = 20
// Call prime() passing argument
// N which is equal to 20 and
// store the result in an array
const primeAry = await prime(N)
// Print the array
console.log(primeAry)
}
catch (e) {
// Display the error in case
// anything wrong happened
console.log(e)
}
}
// Calling findPrime() method
findPrime()
输出:
[
2, 3, 5, 7,
11, 13, 17, 19
]