es6的promise对象解决了js异步回调函数多重嵌套的的噩梦,不再用写像这样的代码node
query('select * from user',function(err,data) { query('select * from user1',function(err,data) { query('select * from user2',function(err,data) { //无穷无尽的异步操做。。。 }) }) })
而能够像这样。es6
query('select * from user') .then(function(data) { return query('select * from user1') }) .then(function(data) { return query('select * from user2') }) .then(function(data) { //后续操做。。。 })
代码的可读性获得了大大的提高,而且更容易维护。可是promise并非万能的,好比说在循环中有多个异步操做,有无穷多个then函数就比较麻烦了,好比这样promise
//查询订单及每一个订单的每一个商品 query('select * from order') .then(data => { var orders = data for(let order of orders) { query('select * from order_prod where oid='+order.oid) .then(data => { order.prods = data }) } //最终须要获得结果 console.log(orders) })
这里须要获得最终的结果就比较使人头疼了,好在es7的async/await异步方案为咱们提供了解决方案。node.js7.6已经原生支持async/await,因此把node更新到7.6以上,就能够放心使用啦。async 能够声明一个异步函数,此函数须要返回一个 Promise 对象。await 能够等待一个 Promise 对象 resolve,并拿到结果。好比这样异步
async function sleep(timeout) { return new Promise((resolve, reject) => { setTimeout(function() { resolve(); }, timeout); }); } (async function() { console.log('Do some thing, ' + new Date()); await sleep(3000); console.log('Do other things, ' + new Date()); })(); //Do some thing, Mon Feb 23 2015 21:52:11 GMT+0800 (CST) //Do other things, Mon Feb 23 2015 21:52:14 GMT+0800 (CST)
因此在循环promise取得最终结果就能够是这样 async
let getOrderProd = async data => { var order = data for(let order of orders) { //将以前封装的基于promised的query函数还原成普通的异步回调模式 await f(order) } //最终须要获得结果 console.log(orders) return new Promise((reslove,reject) => { reslove(orders) }) } function f(i) { return new Promise((resolve,reject) => { query1('select * from order_prod where oid='+order.oid, function(err,data){ i.prods = data resolve(i) }) }) } query('select * from order') .then(getOrderProd).then(data => { //后续操做。。。。。 })
这样循环异步操做的问题就解决了。函数