Code Monkey home page Code Monkey logo

Comments (6)

yortus avatar yortus commented on July 1, 2024

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.

labs20 avatar labs20 commented on July 1, 2024

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.

labs20 avatar labs20 commented on July 1, 2024
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.

yortus avatar yortus commented on July 1, 2024

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.

labs20 avatar labs20 commented on July 1, 2024

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.

yortus avatar yortus commented on July 1, 2024

Thanks for the kind words, and you are most welcome.

from asyncawait.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.