Javascript Node.js Array.map() 是异步的吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12763613/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 11:12:23  来源:igfitidea点击:

Is Node.js Array.map() asynchronous?

javascriptnode.jsasynchronouscallbackmapping

提问by bonchef

Can I count on nodeIDs mapping is completed every time doSomething() is called?

我可以指望每次调用 doSomething() 时都完成 nodeIDs 映射吗?

nodeIDs = $.map(nodeIDs, function(n){
    return n.match(/\d+$/);
});
doSomething(nodeIDs);

I thought all callbacks in node.js are asynchronous? I did read an article on general programming that callback could be synchronous but I am not sure about node.js?

我认为 node.js 中的所有回调都是异步的?我确实读过一篇关于一般编程的文章,回调可以是同步的,但我不确定 node.js?

回答by nalply

JavaScript is also a functional programming language. What you have here is a ?higher order function?, a function which takes a function as a parameter. Higher order functions are synchronous (but see note below).

JavaScript 也是一种函数式编程语言。你在这里拥有的是一个“高阶函数”,一个将函数作为参数的函数。高阶函数是同步的(但请参见下面的注释)。

Sources:

资料来源:

map()is a typical example of a higher order function. It takes a function and applies it to all elements of an array. The definition sounds very ?functional?. This function is also not provided by Node. It is documented by MDN Array.prototype.map()and specified by ECMAScript 5.1.

map()是高阶函数的典型例子。它接受一个函数并将其应用于数组的所有元素。这个定义听起来很“功能性”。Node.js 也没有提供这个功能。它由MDN Array.prototype.map() 记录并由ECMAScript 5.1指定。

To answer your question: Yes, doSomething(nodeIDs)is called afterall elements have been applied.

回答您的问题:是的,在应用所有元素doSomething(nodeIDs)调用。



Note:注意:高阶函数是函数式编程的一个概念。JavaScript 是功能性的,但也深深植根于在浏览器内或服务器上执行代码的实用性。我会说,例如,setTimeout()setTimeout()即使它接受一个函数作为参数,setTimeout()setTimeout()它也不是一个高阶函数,因为它不是真正的纯函数,因为它使用时间。纯粹的功能是永恒的。例如,结果map()map()不依赖于时间。这就是这个问题的真正意义所在。如果某些内容不依赖于时间,则同步执行它。问题解决了。

Thanks to Simon for challenging the definition of the higher order function in JavaScript.

感谢 Simon 挑战 JavaScript 中高阶函数的定义。

回答by bonchef

Yes, .mapis synchronous. "Callback" does not imply "asynchronous".

是的,.map是同步的。“回调”并不意味着“异步”。

回答by Damien Romito

import the asyncmodule to have an asynchronous 'map' method

导入async模块以具有异步“ map”方法

var async = require('async');

var arr = ['1','2'];
async.map(arr, getInfo, function (e, r) {
  console.log(r);
});

function getInfo(name, callback) {
  setTimeout(function() {
    callback(null, name + 'new');
  }, 1000);
}

回答by ThiefMaster

This function is synchronous - otherwise it couldn't return the result of the map operation.

此函数是同步的 - 否则无法返回映射操作的结果。

Any callbacks that might take longer time (mainly due to IO) are asynchronous in nodejs - unless the method is explicitely marked as being synchronous (such as fs.readFileSync- but that doesn't use a callback). You probably confused that somehow.

任何可能需要更长时间(主要是由于 IO)的回调在 nodejs 中都是异步的 - 除非该方法被明确标记为同步(例如fs.readFileSync- 但不使用回调)。你可能以某种方式混淆了这一点。

回答by Roberto A. Batty

use a forof (is synchronous):

使用 forof(同步):

let arr = ['fizz', 'buzz']
//example
    for (const item of arr) {
   //this Examp_func returns array
     console.log((await Examp_func(item )).length);
    }

回答by Ivan Nosov

There is useful lib awaiting. And mapwill help you.

有有用的 lib等待。而地图会帮助你。