Skip to content

Instantly share code, notes, and snippets.

@meathill
Created February 16, 2016 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save meathill/14368b26d6a467461551 to your computer and use it in GitHub Desktop.
Save meathill/14368b26d6a467461551 to your computer and use it in GitHub Desktop.
make FileReader thenable
function reader (file, options) {
options = options || {};
return new Promise(function (resolve, reject) {
let reader = new FileReader();
reader.onload = function () {
resolve(reader);
};
reader.onerror = reject;
if (options.accept && !new RegExp(options.accept).test(file.type)) {
reject({
code: 1,
msg: 'wrong file type'
});
}
if (!file.type || /^text\//i.test(file.type)) {
reader.readAsText(file);
} else {
reader.readAsDataURL(file);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment