search

Home  >  Q&A  >  body text

node.js - nodejs 7 中的async,await到底应该怎么用?

我写的这个函数:

var mysql=require('promise-mysql');

var pool=mysql.createPool({});

async function query() {
    let rows= await pool.query('select * from test');
    
    return rows;
}



满以为会返回真正的记录,结果返回的还是一个promise对象,难道nodejs真的不能实现像java一样的同步代码了吗?
大家讲道理大家讲道理2903 days ago569

reply all(1)I'll reply

  • PHPz

    PHPz2017-04-17 15:28:08

    async will definitely return Promise. Adding await will return the direct result. However, await can only appear in async function...

    So, the innermost async function must return a Promise (or a direct quantity, which will be encapsulated into a Promise), and the outermost layer must also get a Promise.

    reply
    0
  • Cancelreply