javascript - How to wait for forEach to complete when each iteration calls an asynchronous options? -
alright, here's plan is. go through each file, add file array. once files added, combine them using jsziputility , docxtemplater:
'click .merge-icon': (e) => { var programid = router.current().url.split('/').pop(); var programobj = programs.findone(programid); var inserteddocuments = []; var = 0; var count = programobj.activityids.count; var filedownloadpromise = new promise((resolve, reject) => { programobj.activityids.foreach(function(activityid) { var activityobj = activities.findone(activityid); var documentobj = activityfiles.findone(activityobj.documents.pop()._id); jsziputils.getbinarycontent(documentobj.url(), callback); function callback(error, content) { var zip = new jszip(content); var doc = new docxtemplater().loadzip(zip); var xml = zip.files[doc.filetypeconfig.textpath].astext(); xml = xml.substring(xml.indexof("<w:body>") + 8); xml = xml.substring(0, xml.indexof("</w:body>")); xml = xml.substring(0, xml.indexof("<w:sectpr")); inserteddocuments.push(xml); i++; if (i == count - 1) { resolve(); } } }); }); filedownloadpromise.then(() => { jsziputils.getbinarycontent('/assets/template.docx', callback); function callback(error, content) { console.log(content); var zip = new jszip(content); var doc = new docxtemplater().loadzip(zip); setdata(doc); } function setdata(doc) { doc.setdata({ body: inserteddocuments.join('<w:br/><w:br/>') }); doc.render(); useresult(doc); } function useresult(doc) { var out = doc.getzip().generate({ type: 'blob', mimetype: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' }); saveas(out, programobj.name + '.docx'); } }); }
turns out nothing's happening. what's wrong execution of promise here ?
i'm calling resolve when every file has been loaded in array.
Comments
Post a Comment