Skip to content

Instantly share code, notes, and snippets.

@iansan5653
Created July 17, 2019 13:33
Show Gist options
  • Save iansan5653/f43a8f715131920df037e13d79c17715 to your computer and use it in GitHub Desktop.
Save iansan5653/f43a8f715131920df037e13d79c17715 to your computer and use it in GitHub Desktop.
Promisified csv-parse method
/**
* 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