Skip to content

Commit 932aef7

Browse files
committed
papa-parse.start will return a Promise if parse via URL or File
1 parent 8b8c218 commit 932aef7

File tree

2 files changed

+40
-26
lines changed

2 files changed

+40
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ PolymerVis is a personal project and is NOT in any way affliated with Papaparse,
6565
`papa-parse` is a Polymer 2.0 element to parse CSV files into JSON object(s)
6666
with [Papa parse](http://papaparse.com/).
6767

68-
`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.
68+
`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.
6969

7070
Parse from URL.
7171
```html

papa-parse.html

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,9 @@
386386
/**
387387
* If `auto` flag is not set, use this function to start the job manually.
388388
* Will resume parsing instead if parsing was paused previously.
389-
* Only return results if `raw` is being parsed.
390-
* @return {Array[]|Object[]|undefined}
389+
* return results if `raw` is being parsed, otherwise return a Promise to the results
390+
*
391+
* @return {Array[]|Object[]|Promise}
391392
*/
392393
start() {
393394
if (this.stream && this._parser) return this.resume();
@@ -425,30 +426,34 @@
425426
}
426427

427428
_parseRaw(raw, _config, run) {
428-
if (!raw || !this._attached || !run) return;
429+
if (!raw || !run) return;
429430
var results = Papa.parse(raw, _config);
430431
this._syncProperties(results);
431432
return results;
432433
}
433434

434435
_parseFile(file, _config, run) {
435-
if (!file || !this._attached || !run) return;
436-
var config = Object.assign(
437-
{complete: this._complete.bind(this)},
438-
this._config
439-
);
440-
441-
Papa.parse(file, config);
436+
if (!file || !run) return;
437+
return new Promise((resolve, reject) => {
438+
let config = Object.assign(
439+
{complete: this._complete(this, resolve, reject)},
440+
this._config
441+
);
442+
443+
Papa.parse(file, config);
444+
});
442445
}
443446

444447
_parseUrl(url, _config, run) {
445-
if (!url || !this._attached || !run) return;
446-
var config = Object.assign(
447-
{download: true, complete: this._complete.bind(this)},
448-
this._config
449-
);
450-
451-
Papa.parse(url, config);
448+
if (!url || !run) return;
449+
return new Promise((resolve, reject) => {
450+
let config = Object.assign(
451+
{download: true, complete: this._complete(this, resolve, reject)},
452+
this._config
453+
);
454+
455+
Papa.parse(url, config);
456+
});
452457
}
453458

454459
_syncProperties(results) {
@@ -467,17 +472,26 @@
467472
this.dispatchEvent(new CustomEvent('stream', {detail: results}));
468473
}
469474

470-
_complete(results) {
471-
this._syncProperties(results);
475+
_complete(self, resolve, reject) {
476+
return function(results) {
477+
this._syncProperties(results);
472478

473-
if (this.errors && this.errors.length > 0)
474-
return this.dispatchEvent(new CustomEvent('errored', {detail: results}));
479+
if (this.errors && this.errors.length > 0) {
480+
this.dispatchEvent(new CustomEvent('errored', {detail: results}));
481+
return reject(results);
482+
}
483+
484+
if (results && results.meta && results.meta.aborted) {
485+
this.dispatchEvent(new CustomEvent('aborted', {detail: results}));
486+
return reject(results);
487+
}
475488

476-
if (results && results.meta && results.meta.aborted)
477-
return this.dispatchEvent(new CustomEvent('aborted', {detail: results}));
489+
if (results) {
490+
this.dispatchEvent(new CustomEvent('completed', {detail: results}));
491+
}
478492

479-
if (results)
480-
this.dispatchEvent(new CustomEvent('completed', {detail: results}));
493+
resolve(results);
494+
}.bind(self);
481495
}
482496
}
483497

0 commit comments

Comments
 (0)