Do async in a blocking program language way?

后端 未结 1 1695
执念已碎
执念已碎 2021-01-26 05:10
  1. Sync way

For example, ruby:

con = Mysql.new(\'localhost\') 
rs1 = con.query(\'select * from test01\')  # A
rs2 = con.query(\'select * from t         


        
相关标签:
1条回答
  • 2021-01-26 05:56

    My question is, does it have to be in this way? Is it possible by writing sync code but get a async effect?

    Yes. You can use async/await on Node 7+ or generator based coroutines on older versions of Node.

    It will look something like:

    var x = await f1();
    var y = await f2(x);
    // ...
    

    or even:

    var y = await f2(await f1());
    // ...
    

    but it will still be asynchronous and non-blocking. You can use that syntax with any function that returns a promise - like a lot of the database drivers and ORMs/ODMs in Node do.

    See those answers for more examples:

    • How do I send multiple queries from one endpoint with Express?
    • I can't get the value of "result" in Node.js
    0 讨论(0)
提交回复
热议问题