Path: csiph.com!xmission!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail From: Jake Jarvis Newsgroups: comp.lang.javascript Subject: Re: Array of Files to array of data url Date: Tue, 19 Jun 2018 12:28:52 +0200 Organization: A noiseless patient Spider Lines: 31 Message-ID: References: <8dGdnZA1WKvpTbXGnZ2dnUU7-e3NnZ2d@westnet.com.au> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 19 Jun 2018 10:28:51 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="832fcc06ea533265a9eed92a23d68efc"; logging-data="17839"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/2/tcPGZyX/ezAZ8NsMTakqDCP9u/VuTY=" User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 Cancel-Lock: sha1:zWOUHrV6sAO94c1DJ0DzH1pkDf4= In-Reply-To: <8dGdnZA1WKvpTbXGnZ2dnUU7-e3NnZ2d@westnet.com.au> Xref: csiph.com comp.lang.javascript:35239 Am 19.06.2018 um 11:44 schrieb Andrew Poulos: > I have this array > > var files = [File, File, null, File]; > > and I need to create a new array with the data from each File (or null > if it's null). > > I tried > > var newarr = files.map(function(el){ >   if (el === null) return null; > >   var reader = new FileReader(); > >   reader.addEventListener("load", function () { >     return reader.result; >   }, false); > >   reader.readAsDataURL(el); > }); > > but the array only ever gets populated with null. How can this be done? > > Andrew Poulos File API is asynchronous. ("Don't call us, we'll call you") No idea what the big picture is, maybe those new Promise things can help.