How can I export promise result?

前端 未结 4 1107
南笙
南笙 2021-01-07 23:12

Sorry if this question is stupid.

This code works correctly. And I just need to export data variable after all promises successfully resolved.

I cannot put t

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-07 23:55

    Nowadays u do

    foo.js

    const wait = ms => new Promise(resolve => setTimeout(resolve, ms))
    
    async function foo() {
        console.log('called')
        await wait(1000)
        return 'hi'
    }
    
    export default foo()
    

    index.js

    import foo from './foo'
    
    void (async function() {
        console.log(foo)
        console.log(await foo)
        console.log(await foo)
    })()
    

    output

    > called
    > Promise {  }
    > hi
    > hi
    

    It's usefull to fetch datas at start

提交回复
热议问题