Comments (6)
Hi @labs20,
Try this:
var test = async (function () {
var sql = 'select * from my_sqlserver_table';
console.log('1 - Start');
Seriate.setDefaultConfig(config_obj_set_elsewhere);
var r = await (Seriate.execute({ query: sql }));
console.log('2 - Results: ' + r);
console.log('3 - End');
});
test();
Let me know if you need further explanation, happy to help.
from asyncawait.
Hi @yortus , thanks for your quick answer, but thats still not what I want to achieve. Let me try to be more clear and perhaps you can help me further:
var x = 2;
console.log('x is: ' + x);
var getTotal = async(function(id){
var sql = 'select sum(*) as total from debits_table where id = ' + id;
Seriate.setDefaultConfig(config_obj_set_elsewhere);
var r = await (Seriate.execute({ query: sql }));
return r['total'];
});
x += getTotal(3);
console.log('the sync value of x is: '+ x); // will print [object Promise]
This is a naive code and not my real case scenario but i think it will do better to explain what I'm trying to do. I want a real synchronous function to do a database query to feed a simple var with something from the db, and then maybe feed one or two more, analise them and do other things, and then, move on.
As it is, the last console.log is reached before getTotal has had a chance to run the query.
Thanks again!
from asyncawait.
console.log('0 - begin');
var test = async (function () {
var sql = 'select * from my_sqlserver_table';
console.log('1 - Start');
Seriate.setDefaultConfig(config);
var r = await (Seriate.execute({ query: sql }));
console.log('2 - Results: ' + r);
console.log('3 - End');
});
test();
console.log('4 - Finish');
Prints: 0, 1, 4, 2, 3
=[
(whats wrong with the code formating tag, anyway? =/ )
from asyncawait.
For code formatting, use triple backticks (```). I've updated your previous two comments accordingly.
OK, I think I understand what you are asking for, and there's good and bad news. The bad news is that asyncawait
is non-blocking, so it cannot do what you are asking. Your getTotal
function returns a promise of a result, not the actual result. The 'illusion' of blocking at each await
only holds inside the async
function body. But to the outside world, the function is an ordinary function that returns a promise which will resolve to a result (or an error) at some future point in time when that body of the function has finished executing (or thrown an error).
The good news is that it's still possible to achieve the kind of thing you want to do, you just have work with the non-blocking nature of node. Anywhere you want to use 'blocking' semantics, you have to wrap up that code inside an async
function. You can call such a function from anywhere in your code, and it will return a promise.
So with your getTotal
example, you'd do somthing like:
var main = async (function () {
var x = 2;
console.log('x is: ' + x);
x += await (getTotal(3));
console.log('the sync value of x is: '+ x);
});
var getTotal = async(function(id){
var sql = 'select sum(*) as total from debits_table where id = ' + id;
Seriate.setDefaultConfig(config_obj_set_elsewhere);
var r = await (Seriate.execute({ query: sql }));
return r['total'];
});
main()
.then(function (result) { console.log('main finished'); })
.catch(function (error) { console.log('main threw an error'); });
from asyncawait.
Thank you @yortus very much for your explanation.
After I posted my comment, I start to realize that my way of thinking things is wrong. I'm new to node so I'm still fighting concepts like "async always", when the best to do is just embrace those new concepts.
I'm redoing my code like a politician, "promises" everywhere =]
After your kind post I can see now that sync situations while still valid, are much less required than I thought.
Thank you, you are very kind and helpfull.
from asyncawait.
Thanks for the kind words, and you are most welcome.
from asyncawait.
Related Issues (20)
- Use with ES6 classes #2 HOT 1
- Not working on nw.js HOT 1
- Error: await functions, yield functions, and value-returning suspendable functions. HOT 1
- setTimout example? HOT 2
- Unexpected token function HOT 6
- Fiber not running HOT 1
- why so long time require this two packages? HOT 1
- Extra parentheses? HOT 2
- fibers v1.0.16 dependency breaks node 4.x HOT 1
- Using a for loop HOT 2
- await inside an awaited function
- Stuck within await()... HOT 2
- Installation issue on node version 9.0.0 HOT 1
- Using forEach with Callbacks function
- Not an issue, just a thank you. HOT 1
- asyncawait fails to build on Ubuntu 16.04.4 LTS HOT 3
- Examples in typescript HOT 1
- Upgrade fibers package for nodejs 10.x or 12.x HOT 6
- Await behavior in non Async function or normal function HOT 1
- Not install in latest node/npm. HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from asyncawait.