看起来你需要
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! :)");