Skip to content

Instantly share code, notes, and snippets.

@viggi-v
Created June 25, 2017 13:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save viggi-v/cf996ad839a7b9525dc0e1ea89d45346 to your computer and use it in GitHub Desktop.
Save viggi-v/cf996ad839a7b9525dc0e1ea89d45346 to your computer and use it in GitHub Desktop.
Sample demo of promises in js
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>
Head
</title>
<script>
function getTextFromFile(fileEvent){
return new Promise(function(resolve,reject){
var input = fileEvent.target;
var reader = new FileReader();
reader.onload = function(){
resolve(reader.result);
};
reader.onerror = function(){
reject(Error(reader.error));
};
reader.readAsText(input.files[0])
});
}
function openFile(event){
getTextFromFile(event).then(function(text){
document.getElementById("op").innerHTML = "The text is <hr>" + text;
}).catch(function(err){
console.log("Error:"+err);
});
}
</script>
<link rel="stylesheet" href="">
</head>
<body>
<input type='file' accept='text/plain' onchange='openFile(event)'>
<div id="op">
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment