Created
June 25, 2017 13:10
-
-
Save viggi-v/cf996ad839a7b9525dc0e1ea89d45346 to your computer and use it in GitHub Desktop.
Sample demo of promises in js
This file contains 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
<!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