为什么表示app.render与本地文件path同步加载图像? – NodeJS
我注意到以下几点:
- 当我想渲染一个像这样的另一个网站托pipe的图像URL数组:
index.js
MongoClient.connect('mongodb://localhost:27017/sample', function(err, db) { db.collection('collSample').findOne( { somevar: 'somevalue' }, function(err, doc) { assert.equal(null, err); res.render('index', { title: 'render images' , imagesPaths: doc.images.`**URLs**` }); db.close(); }); });
index.jade
- each path in imagesPaths span img(src="#{path}")
我从Chrome开发者工具中获得这个networking信息。 注意图像是如何asynchronous渲染的
- 但是,如果我使用本地保存的图像,如下所示:
index.js
MongoClient.connect('mongodb://localhost:27017/sample', function(err, db) { db.collection('collSample').findOne( { somevar: 'somevalue' }, function(err, doc) { assert.equal(null, err); res.render('index', { title: 'render images' , imagesPaths: doc.images.`**localPath**` }); db.close(); }); });
index.jade
- each path in imagesPaths span img(src="#{path}")
我得到这个networking信息。 请注意,iamges正在同步呈现。 另外, 加载时间也相当低 。
问题1 :如何强制渲染图像asynchronous?
问题2 :为什么有时nodejs渲染图像asynchronous和其他人同步? 我认为nodejs将会asynchronous渲染一堆图像,而不pipe图像的src属性是什么。
- 它们不是同步加载的 。 所有请求都是asynchronous的(请参阅所有的白色矩形开始同时打印)。
真正的区别在于对资源位置的请求path:
-
每个远程请求都通过互联网,并不保证到达顺序在开始时是一样的。
-
每一个本地请求都会立即处理,让幻觉变成同步,也保持了请求的顺序。
certificate:尝试添加一个非常大的图像o(N * 10MB)或o(100MB)的本地path(例如http://www.nasa.gov/sites/default/files/thumbnails/image/hs-2015-02 -a-hires_jpg.jpg )在数组的中间,看到图像完成最后加载。