From 932aef7dd1d0609d0841dc1b364e7afd0d2c8c0e Mon Sep 17 00:00:00 2001 From: eterna2 Date: Tue, 10 Oct 2017 11:14:52 +0800 Subject: [PATCH] papa-parse.start will return a Promise if parse via URL or File --- README.md | 2 +- papa-parse.html | 64 ++++++++++++++++++++++++++++++------------------- 2 files changed, 40 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index a48621b..d9d5324 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ PolymerVis is a personal project and is NOT in any way affliated with Papaparse, `papa-parse` is a Polymer 2.0 element to parse CSV files into JSON object(s) with [Papa parse](http://papaparse.com/). -`papa-parse` can download and parse a csv file via `url`, or from raw csv strings via `raw`, and File object via `file`. If the `auto` flag is set, `papa-parse` will automatically start the job, otherwise a manual call to the function `parse` will be needed. +`papa-parse` can download and parse a csv file via `url`, or from raw csv strings via `raw`, and File object via `file`. If the `auto` flag is set, `papa-parse` will automatically start the job, otherwise a manual call to the function `start` will be needed. Parse from URL. ```html diff --git a/papa-parse.html b/papa-parse.html index 29d4f79..d9f4347 100644 --- a/papa-parse.html +++ b/papa-parse.html @@ -386,8 +386,9 @@ /** * If `auto` flag is not set, use this function to start the job manually. * Will resume parsing instead if parsing was paused previously. - * Only return results if `raw` is being parsed. - * @return {Array[]|Object[]|undefined} + * return results if `raw` is being parsed, otherwise return a Promise to the results + * + * @return {Array[]|Object[]|Promise} */ start() { if (this.stream && this._parser) return this.resume(); @@ -425,30 +426,34 @@ } _parseRaw(raw, _config, run) { - if (!raw || !this._attached || !run) return; + if (!raw || !run) return; var results = Papa.parse(raw, _config); this._syncProperties(results); return results; } _parseFile(file, _config, run) { - if (!file || !this._attached || !run) return; - var config = Object.assign( - {complete: this._complete.bind(this)}, - this._config - ); - - Papa.parse(file, config); + if (!file || !run) return; + return new Promise((resolve, reject) => { + let config = Object.assign( + {complete: this._complete(this, resolve, reject)}, + this._config + ); + + Papa.parse(file, config); + }); } _parseUrl(url, _config, run) { - if (!url || !this._attached || !run) return; - var config = Object.assign( - {download: true, complete: this._complete.bind(this)}, - this._config - ); - - Papa.parse(url, config); + if (!url || !run) return; + return new Promise((resolve, reject) => { + let config = Object.assign( + {download: true, complete: this._complete(this, resolve, reject)}, + this._config + ); + + Papa.parse(url, config); + }); } _syncProperties(results) { @@ -467,17 +472,26 @@ this.dispatchEvent(new CustomEvent('stream', {detail: results})); } - _complete(results) { - this._syncProperties(results); + _complete(self, resolve, reject) { + return function(results) { + this._syncProperties(results); - if (this.errors && this.errors.length > 0) - return this.dispatchEvent(new CustomEvent('errored', {detail: results})); + if (this.errors && this.errors.length > 0) { + this.dispatchEvent(new CustomEvent('errored', {detail: results})); + return reject(results); + } + + if (results && results.meta && results.meta.aborted) { + this.dispatchEvent(new CustomEvent('aborted', {detail: results})); + return reject(results); + } - if (results && results.meta && results.meta.aborted) - return this.dispatchEvent(new CustomEvent('aborted', {detail: results})); + if (results) { + this.dispatchEvent(new CustomEvent('completed', {detail: results})); + } - if (results) - this.dispatchEvent(new CustomEvent('completed', {detail: results})); + resolve(results); + }.bind(self); } }