我在数组上进行映射,对于新对象的返回值之一,我需要进行异步调用。
var firebaseData = teachers.map(function(teacher) { return { name: teacher.title, description: teacher.body_html, image: urlToBase64(teacher.summary_html.match(/src="(.*?)"/)[1]), city: metafieldTeacherData[teacher.id].city, country: metafieldTeacherData[teacher.id].country, state: metafieldTeacherData[teacher.id].state, studioName: metafieldTeacherData[teacher.id].studioName, studioURL: metafieldTeacherData[teacher.id].studioURL } });
该功能的实现将类似于
function urlToBase64(url) { request.get(url, function (error, response, body) { if (!error && response.statusCode == 200) { return "data:" + response.headers["content-type"] + ";base64," + new Buffer(body).toString('base64'); } }); }
我不清楚做到这一点的最佳方法是什么…承诺?嵌套的回调?在ES6或ES7中使用某些东西,然后与Babel进行转换?
目前实现此目的的最佳方法是什么?
谢谢!
一种方法是Promise.all(ES6)。
Promise.all
该答案将在Node 4.0+中起作用。较旧的版本将需要Promise polyfill或库。我还使用了ES6箭头功能,function对于Node <4 ,可以将其替换为常规s。
function
该技术request.get用Promise 手动包装。您还可以使用诸如request- promise之类的库。
request.get
function urlToBase64(url) { return new Promise((resolve, reject) => { request.get(url, function (error, response, body) { if (!error && response.statusCode == 200) { resolve("data:" + response.headers["content-type"] + ";base64," + new Buffer(body).toString('base64')); } else { reject(response); } }); }) } // Map input data to an Array of Promises let promises = input.map(element => { return urlToBase64(element.image) .then(base64 => { element.base64Data = base64; return element; }) }); // Wait for all Promises to complete Promise.all(promises) .then(results => { // Handle results }) .catch(e => { console.error(e); })