不知道如何在 node.js 中使用 synchronize.js
Can't figure out how to use synchronize.js in node.js
我正在尝试在我的 node.js 应用程序中使用模块 synchronize.js,但我失败了。功能仍然 运行 乱序,我没有得到我的数据。
这是我的代码的一般片段。
// an object with devices to migrate
var devices2Migrate =
{
device_array: [],
init: function ( done )
{
sync.fiber(function()
{
//do init stuff
console.log( 'yay we have a DB connector' );
DB1.connect(function(err)
{
console.log( 'Database 1 is connected ...' );
}
DB2.connect(function(err)
{
console.log( 'Database 2 is connected ...' );
}
}, done);
},
loadDevices2Migrate: function ( done )
{
var self = this;
sync.fiber(function()
{
//get stuff from DB using query()
}, done);
}
}; //end of object
// load up all the data for the devices using the old style tables
sync.fiber(function(){
sync.await( devices2Migrate.init( sync.defer() ));
sync.await( devices2Migrate.loadDevices2Migrate( sync.defer() ) );
console.log( devices2Migrate.device_array );
console.log( "size: " + devices2Migrate.device_array.length );
});
但是 console.log 表明函数没有等待:
yay we have a DB connector
[]
size: 0
Database 1 is connected ...
Database 2 is connected ...
谁能指出我做错了什么?如果需要,我可以填写更多的方法体,一开始我只是想保持简单。
我知道我的方法(函数)是正确的,因为如果我将底部的 console.log 调用(纤程中的调用)包装在 setTimeout 回调中,那么我会得到我的数据。
使用 Node 时,this is my favourite package 承诺:
简单的承诺:
return when.promise(function(resolveCallback, rejectCallback) {
//do something here
var blah = foo("bar");
//return callback like this
resolveCallback();
});
链式承诺:
return when.promise(函数(resolveCallback, rejectCallback) {
//在这里做点什么
var blah = foo("bar");
//return callback like this
resolveCallback();
}).then(x);
var x = function(){
return when.promise(function(resolveCallback, rejectCallback) {
//do something here
var blah = foo("bar");
//return callback like this
resolveCallback();
});
};
我最终使用了 async.js 包。更重要的是 "user friendly" 它起作用了!
编辑:
在异步中,我这样做了:
// load up all the data for the devices using the old style tables
async.series(
devices2Migrate.init( callback ),
devices2Migrate.loadDevices2Migrate( callback )
){
// land here when done
console.log( devices2Migrate.device_array );
console.log( "size: " + devices2Migrate.device_array.length );
});
我正在尝试在我的 node.js 应用程序中使用模块 synchronize.js,但我失败了。功能仍然 运行 乱序,我没有得到我的数据。 这是我的代码的一般片段。
// an object with devices to migrate
var devices2Migrate =
{
device_array: [],
init: function ( done )
{
sync.fiber(function()
{
//do init stuff
console.log( 'yay we have a DB connector' );
DB1.connect(function(err)
{
console.log( 'Database 1 is connected ...' );
}
DB2.connect(function(err)
{
console.log( 'Database 2 is connected ...' );
}
}, done);
},
loadDevices2Migrate: function ( done )
{
var self = this;
sync.fiber(function()
{
//get stuff from DB using query()
}, done);
}
}; //end of object
// load up all the data for the devices using the old style tables
sync.fiber(function(){
sync.await( devices2Migrate.init( sync.defer() ));
sync.await( devices2Migrate.loadDevices2Migrate( sync.defer() ) );
console.log( devices2Migrate.device_array );
console.log( "size: " + devices2Migrate.device_array.length );
});
但是 console.log 表明函数没有等待:
yay we have a DB connector
[]
size: 0
Database 1 is connected ...
Database 2 is connected ...
谁能指出我做错了什么?如果需要,我可以填写更多的方法体,一开始我只是想保持简单。 我知道我的方法(函数)是正确的,因为如果我将底部的 console.log 调用(纤程中的调用)包装在 setTimeout 回调中,那么我会得到我的数据。
使用 Node 时,this is my favourite package 承诺:
简单的承诺:
return when.promise(function(resolveCallback, rejectCallback) {
//do something here
var blah = foo("bar");
//return callback like this
resolveCallback();
});
链式承诺:
return when.promise(函数(resolveCallback, rejectCallback) { //在这里做点什么 var blah = foo("bar");
//return callback like this
resolveCallback();
}).then(x);
var x = function(){
return when.promise(function(resolveCallback, rejectCallback) {
//do something here
var blah = foo("bar");
//return callback like this
resolveCallback();
});
};
我最终使用了 async.js 包。更重要的是 "user friendly" 它起作用了!
编辑: 在异步中,我这样做了:
// load up all the data for the devices using the old style tables
async.series(
devices2Migrate.init( callback ),
devices2Migrate.loadDevices2Migrate( callback )
){
// land here when done
console.log( devices2Migrate.device_array );
console.log( "size: " + devices2Migrate.device_array.length );
});