等待所有promises并启动一个函数(渲染前的图像缓存)

时间:2017-12-20 11:26:24

标签: javascript promise

目标是创建一个图像缓存预加载的图像,以便在不使用函数init时更新它们。因此,准备图像以供进一步使用。 目前我已经有了这段代码:

var links = [
  'https://get.wallhere.com/photo/trees-forest-fall-waterfall-water-nature-rocks-national-park-pond-jungle-stream-rainforest-watercourse-wasserfall-habitat-natural-environment-body-of-water-water-feature-57484.jpg',
  'http://ru.artsdot.com/ADC/Photos-ImgScreen.nsf/O/P-AC6V56/$FILE/Buildings-night_city_65_.Jpg',
  'https://cdn.wallaps.com/wallpapers/10000/2870.jpg',
  'http://on-desktop.com/wps/Nature_Beach_Bright_paints_of_coast_023115_.jpg',
  'http://wallpaperscraft.ru/image/ravnina_zelen_solnce_gory_derevya_trava_326_2560x1600.jpg'
];
const checkImage = (path) => {
	const promise = new Promise(resolve => {
		const img = new Image();
		
		img.onload = () => resolve({path, status: 'ok'});
		img.src = path
	})
	promise.then(result => console.log('...image loaded'))
}
	
const loadImg = (paths) => Promise.all(paths.map(checkImage)).then(result => console.log('here we start a function'));
loadImg(links)

但是“.then(这里我们开始一个函数)”在任何承诺准备好之前开始?我在解决这个问题的逻辑中缺少什么?

0 个答案:

没有答案