一个关于async/await异步请求的问题o_0?

nuxt.js 框架的 asyncData方法里面

 let [
        FindInstitutionsDetailed,
        EnterpriseHot,
    ]
    = await Promise.all([
                P.FindInstitutionsDetailed({'id':context.route.query.id+''}),
                P.EnterpriseHot()
            ])
            return {
                ***FindInstitutionsDetailed***: FindInstitutionsDetailed.data,
                EnterpriseHot: EnterpriseHot.data,
            }

如上图,一直是这样写,页面渲染之前会Promise.all 并发请求所有接口。这样是没问题的。

但是,现在有一个接口需要从FindInstitutionsDetailed这个接口里获取userId作为参数。

想问一下该怎么写呢?在nuxt里面访问不到this......
或者在Promise.all(...)后面.then() ???

阅读 2k
2 个回答
let a = api1();
let b = a.then(data=>api2(data.userid))
let c = api3();

lat [dataA,dataB,dataC] = await Promise.all([a,b,c]);

return {
    ...
}

举个例子,你可以这样写

async function f(){
    var result = await new Promise(function(resolve){
        setTimeout(function(){
            resolve('hello')
        })
    })
    await new Promise(function(resolve){
        resolve(result+' world')
    }).then(console.log);
}
f()
推荐问题