angular - How to load large batch of files with FileReader API without locking up the browser in Angular2? -


what "ng2" way of loading large collection of files (+100) using filereader api?

i'd stay of typical recursion , research has led me examples ben nadel's - exploring recursive promises, i'm having hard time figuring out how bring approach ng2 environment.

would recursive promises way go, or there other tools/utilities available within framework can't seem dig in searches? rxjs better suited this?

edit:

so kept thinking more , i'm close, can't seem figure out how ben's approach work async requests:

*note, i'm using lodash reduce, because it's faster.

...  loadfilepreview(files: file[]): promise<any> {     let promise = _.reduce(files,         (promisechain, value) => {             let nextlinkinchain = promisechain.then(                 (previousvalue) => {                     let reader: filereader = new filereader();                      return reader.onload = (readerevent) => {                         return value;                     }                      reader.readasdataurl(value);                 }             );              return(nextlinkinchain);         },          promise.resolve(files[0])     );      return promise; }  ... 

i'm i'm going wrong:

...  return reader.onload  ... 

as ben's example had:

...  return(previousvalue + value);  ... 

and if log example , add logs before return above, this:

value 1 value 2 value 3 complete! 

if switch mine, because it's async request, complete fires before of files read.

thoughts on how can make wait until file loads before goes next chain?

alright, think figured out. way off in edit above.

the following solve seems working fine me @ moment though, here's got:

private loadfiles(files: file[]): promise<any> {    let promise = _.reduce(files, (promisechain, file) => {        let nextlinkinchain = promisechain.then((previousfile) => {            return(this.loadfile(file));        });         return (nextchain);    }, this.loadfile(files[0]));     return promise; }  private loadfile(file: file): promise<any> {     return new promise((resolve, reject) => {         let reader: filereader = new filereader();          reader.onload = (event) => {             resolve(file);         }          reader.readasdataurl(file);     }); } 

this sort of feels recursion though. love know if still true ben's original article in regards chaining or if missed approach entirely.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -