2017-11-22 13 views
0

私はのcreate-react-native-appを使ってプロジェクトをビルドしています。おそらくあなたが知っているように、これはアプリケーションを開発するときに、Expoをフレームワークとして使用します。React-Native Expoアプリasync downloads

私はExpoのFileSystem.downloadAsync()を使用して、必要なファイルをダウンロードしています。私は、各ファイルをダウンロードしたときに知っているが、ときにすべての方法を私は知っています)(.thenで今

this.state.files.map(file => { 
    FileSystem.downloadAsync(file.url, FileSystem.documentDirectory + file.name) 
    .then(({ uri }) => console.log('Saved this file at: ' + uri)) 
    .catch(console.log('Error when downloading file.')) 
}) 

:、)(このようなものを私はダウンロードして複数のファイルを必要とするので、私は.MAPの内側には、このコマンドを実行しますファイルのダウンロードが完了しましたか?

私はそれを関数に入れて呼び出すと、それが終了するのを待たずに次のコードに進むだけです。

どういうわけか、この関数が新しいPromiseを返すようにすることはできますか?それは正しい方法だと思いますが、どうすればいいのでしょうか?どこで解決し、拒否するのですか?

答えて

0

あなたはPromise.all()のように見えます!それは約束の配列を除き、配列の各約束が結果を返すたびに解決する新しい約束を返すでしょう。

preloading and caching assetsのエクスポ文書でここをクリックすると、これの良い例が表示されます。あなたのケースでは

async _loadAssetsAsync() { 
    const imageAssets = cacheImages([ 
     'https://www.google.com/images/branding/googlelogo/2x/googlelogo_color_272x92dp.png', 
     require('./assets/images/circle.jpg'), 
    ]); 

    const fontAssets = cacheFonts([FontAwesome.font]); 

    // Notice how they create an array of promises and then spread them in here... 
    await Promise.all([...imageAssets, ...fontAssets]); 
} 

このようなものは

const assets = this.state.files.map(file => 
    FileSystem.downloadAsync(file.url, FileSystem.documentDirectory + file.name) 
) 
/// Here is where you put the try. 
try { 
    await Promise.all(assets); 
} catch (error) { 
    /// Here is where you would handle an asset loading error. 
    console.error(error) 
} 
console.log("All done loading assets! :)"); 
を働くだろう
関連する問題