Javascript Can';我不知道如何在node.js中使用synchronize.js
我正在尝试使用node.js应用程序中的模块,但失败了。函数仍在无序运行,我无法获取数据。 下面是我的代码的一个常规片段Javascript Can';我不知道如何在node.js中使用synchronize.js,javascript,node.js,asynchronous,synchronize,Javascript,Node.js,Asynchronous,Synchronize,我正在尝试使用node.js应用程序中的模块,但失败了。函数仍在无序运行,我无法获取数据。 下面是我的代码的一个常规片段 // an object with devices to migrate var devices2Migrate = { device_array: [], init: function ( done ) { sync.fiber(function() { //do init stuff console.log( 'yay we ha
// 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回调中,那么我确实会得到我的数据。在使用节点时,对于承诺:
简单的承诺:
return when.promise(function(resolveCallback, rejectCallback) {
//do something here
var blah = foo("bar");
//return callback like this
resolveCallback();
});
连锁承诺:
返回when.promise(函数(resolveCallback、rejectCallback){
//在这里做点什么
var blah=foo(“bar”)
}).然后(x)
我最终使用了这个软件包。它更“用户友好”,因为它工作
编辑:
在async中,我做到了以下几点:
// 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 );
});
@奥利弗,你能提供一个链接吗。在synchronize.js模块中没有提到需要这些东西。表示同步模块不使用/不需要承诺。是否有使用同步模块的原因?如果您愿意帮助举例说明我如何使用承诺,承诺会更容易,更符合“行业标准”?谢谢我将为您提供一个承诺模块和一个示例我的意思是。。。如果你使用得当,同步会起作用的,只是说,谢谢你的否决票@凯文:也许有更好的帮助我度过难关。但正如您所看到的,syncronize.js没有任何答案。我无法使用synchronize解决我的问题,所以我尝试了async.js并按照文档进行操作,结果很好!这就是我的答案,我会坚持下去。谢谢你的帮助!否决票是因为你没有提供解决方案。我确信async.js对您有所帮助(我认为它是synchronize.js的更好的替代方案),但要让答案有用!要点(没有双关语)。我更新了我的答案。
var x = function(){
return when.promise(function(resolveCallback, rejectCallback) {
//do something here
var blah = foo("bar");
//return callback like this
resolveCallback();
});
};
// 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 );
});