nodejs 异步转同步整理
2021-05-29 21:02
标签:结合 参考 property com OLE scroll ext mis contain 主要是集中参考使用方法,可以结合自己的场景解决问题 以上包含了async/await 以及generator,co,async-utility(依赖deasync),同时推荐还是别使用deasync,以下有参考连接 https://joecreager.com/5-reasons-to-avoid-deasync-for-node-js/ nodejs 异步转同步整理 标签:结合 参考 property com OLE scroll ext mis contain 原文地址:https://www.cnblogs.com/rongfengliang/p/14702765.html参考代码
const co =require("co")
const AsyncUtil = require(‘async-utility‘).default;
async function demo(){
return {
name:"dalong",
age:33
}
}
var result =co(function* () {
var result = yield demo();
console.log("co result:",result)
return result;
})
let myresult = AsyncUtil.resolvePromise(result)
console.log("AsyncUtil",myresult)
function* demoapp(){
yield demo();
yield demo();
}
let appdemo = demoapp().next()
appdemo.value.then(data=>{
console.log(data)
})
let myresult2 = AsyncUtil.resolvePromise(appdemo.value)
console.log("generate",myresult2);
(async ()=>{
let info =await demoapp().next().value
console.log("await info",info)
})()
说明
参考资料
https://github.com/tj/co
下一篇:二JS