Created
July 17, 2019 13:33
-
-
Save iansan5653/f43a8f715131920df037e13d79c17715 to your computer and use it in GitHub Desktop.
Promisified csv-parse method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Promisified `parse()` from `csv-parse`. | |
* @param input The raw CSV data. | |
* @param options The options object to pass through to the `parse()` function. | |
* @returns Promise that resolves upon completion with an object containing the | |
* results and information about those results. | |
* @see {@link https://csv.js.org/parse/} For info about the options and info | |
* objects. | |
*/ | |
export function promiseParse( | |
input: string, | |
options: parse.Options | |
): Promise<{records: unknown; info: parse.Info}> { | |
return new Promise((resolve, reject): void => { | |
parse( | |
input, | |
options, | |
(err, records, info): void => { | |
if (err) reject(err); | |
else resolve({records, info}); | |
} | |
); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment