node 中的异步同步取值有时候要用到 async 的用法
async function test() { let data = await new Promise((resolve,reject)=>{ resolve('121211212112121') }) return data } async function getTextVal() { let suatsus= await test(); console.log(suatsus) //121211212112121 } getTextVal()
new Promise 里面的即使是axios请求也没问题, 会等待值 给 之后的data 的,
只是在后面的 地方把 值resolve 进去.
关键是在用的时候 外层跟内层的一定要 套着加 async/await 取如下
async function getTextVal() { let suatsus= await test(); console.log(suatsus) //121211212112121 }