我在双forEach循环内做一些计算,如下所示:
array.forEach(function(element){
Object.keys(element).forEach(function(key){
/* some complex computations with asynchronous callbacks */
});
});
someFunctionHere();
Run Code Online (Sandbox Code Playgroud)
在执行该someFunctionHere( )功能之前,Loop是否有办法先完成?或者任何方式,程序将知道循环是否完成后继续someFunctionHere( )...
我可能会错过一些论坛,但我找到的那些论坛并没有帮助我实现我想要实现的目标,而且我在NodeJS中这样做,我也在问是否有现有的库可以实现这一点.
我忘了添加它或者这是另一个问题吗?
有没有办法同步进行迭代,它只会在当前迭代完成后进入下一次迭代?(非常遗憾)
谢谢你的帮助...
我正在使用plpgsql创建一个存储过程,通过传递一个类型数组并在过程中执行循环,以便我可以插入每个信息类型
CREATE TYPE info AS(
name varchar,
email_add varchar,
contact_no varchar
);
CREATE OR REPLACE FUNCTION insert_info(
info_array info[]
) RETURNS varchar AS $$
DECLARE
info_element info;
BEGIN
FOREACH info_element IN ARRAY info_array
LOOP
INSERT INTO info_table(
name,
email_add,
contact_no
) VALUES(
info_element.name,
info_element.email_add,
info_element.contact_no
);
END LOOP;
RETURN 'OK';
END;
$$ LANGUAGE plpgsql;
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何使用数组输入的函数.我做了一些实验(只有一些愚蠢的输入):
SELECT insert_info(
ARRAY[('Arjay','myEmail@email.com','1234567')]
);
Run Code Online (Sandbox Code Playgroud)
但PostgreSQL说这是一个record[],我还没有测试Loop部分...
我在这个链接中发现了一个类似的问题:
使用%TYPE在PostgreSQL中声明复合类型的变量,
但它没有使用数组.如果这只是一个重复的问题,也许你们可以指出我正确的方向!
我有一个关于Nodejs Fibers的问题(这对我来说绝对是新的)...我有Nodejs Fibers的教程,http: //bjouhier.wordpress.com/2012/03/11/fibers-and-threads-in- node-js-what-for /,这里有一个例子
var fiber = Fiber.current;
db.connect(function(err, conn) {
if (err) return fiber.throwInto(err);
fiber.run(conn);
});
// Next line will yield until fiber.throwInto
// or fiber.run are called
var c = Fiber.yield();
// If fiber.throwInto was called we don't reach this point
// because the previous line throws.
// So we only get here if fiber.run was called and then
// c receives the conn value.
doSomething(c);
// Problem solved!
Run Code Online (Sandbox Code Playgroud)
现在基于这个例子,我创建了我自己的代码版本,
var Fiber = require('fibers'); …Run Code Online (Sandbox Code Playgroud) 我在mongodb中有一个示例文档(我还是mongodb的新手)
{
"ID": 0,
"Facet1":"Value1",
"Facet2":[
{
"Facet2Obj1":{
"Obj1Facet1":"Value11",
"Obj2Facet1":"Value21",
"Obj3Facet1":"Value31"
}
},
{
"Facet2Obj2":{
"Obj1Facet2":"Value12",
"Obj2Facet2":"Value22",
"Obj3Facet2":"Value32"
}
},
{
"Facet2Obj3":{
"Obj1Facet3":"Value13",
"Obj2Facet3":"Value23",
"Obj3Facet3":"Value33"
}
}
],
"Facet3":"Value3"
"Facet4":{
"Facet4Obj1":{
"Obj1Facet1":"Value4111"
}
}
}
Run Code Online (Sandbox Code Playgroud)
Mapreduce有点复杂,它提供以下输出(对于30,000个文档):
{
"_id" : "Facet1",
"value" : [
{
"value" : "Value1",
"count" : 30000,
"ID" : [
0,
1,
.
.
.
]
}
]
}
{
"_id" : "ID",
"value" : [
{
"value" : 0,
"count" : 1,
"ID" : [
0 …Run Code Online (Sandbox Code Playgroud) javascript ×2
node.js ×2
arrays ×1
mongodb ×1
node-fibers ×1
plpgsql ×1
postgresql ×1
unnest ×1