Closed davidemiceli closed 9 years ago
defer
assign to temporary variable. like this;
sync.fiber(function() {
var defer = sync.defer();
knex.raw(...).catch(defer).then(defer);
var data = sync.await();
});
this is not tested. but, I think, it will work :)
Yea, it should be something like that, with supplying null
as the first argument.
sync.fiber(function() {
var defer = sync.defer();
knex.raw(...).catch(defer).then(function(){
arguments.unshift(null)
defer.apply(null, arguments)
});
var data = sync.await();
});
It looks like a lot of code to write, but, all this should be in a helper function (it should be easy to write it), the actual code should looks like:
syncedKnex = syncWithPromises(knex)
sync.fiber(function() {
data = syncedKnex.raw(...)
})
Where syncWithPromises
is the helper function that should be written.
Thanks very much, I tried also with:
var dbquery = function(query, params, fn) {
knex.raw(query, params).catch(function(err) {
console.log(err); return fn(null, err);
}).then(function(resp) {
return fn(null, resp);
});
}
console.log('Starting...');
sync.fiber(function() {
console.log('At first', data);
dbquery('select 1', [], sync.defer());
data = sync.await();
console.log('data', data);
console.log('Last, bye!');
});
And it works
Yep. Also, just a minor fix, it should be:
var dbquery = function(query, params, fn) {
knex.raw(query, params).catch(fn).then(function(resp) {
return fn(null, resp);
});
}
Yes! Thanks!!
Hi, how should synchronize work with asynchronous functions that have promises?
For example, the following case:
For example, in this case, where should I put sync.defer()? How should the code work? Could you help me?
Thanks a lot!