JSFiddle - React, Tailwind, and code Playground

by Oleh Aloshkin

JavaScript

function askForEmail() {
	return new Promise(function(fulfill, reject) {
		promptForText('Enter email:', function(result) {
			if (result.cancel) {
				reject(new Error('User refused to supply email.'));
			} else {
				fulfill(result.text);
			}
		})
	})
}

function promptForText(text, checkResult) {
	var askEmail = prompt(text);
	var result = new Object;
	if (askEmail === null || askEmail == '') {
		result.cancel = true;
	} else {
		result.text = askEmail;
	}
	return checkResult(result);
}

askForEmail().then(
	function fulfilled(email) {
		console.log(email);
	},
	function rejected(err) {
		console.error('Unable to get email: ' + err.message);
	}
)

var root = 'https://jsonplaceholder.typicode.com';

function get(url) {
	return new Promise(function(fulfill, reject) {
		var xhr = new XMLHttpRequest();
		xhr.open('GET', url);
		xhr.onload = function() {
			if (xhr.status >= 400) {
				reject('Post request failed w/ status code ' + xhr.status);
			} else {
				fulfill(xhr.responseText);
			}
		}
		xhr.onerror = function() {
			reject('Post request failed!');
		}
		xhr.send();
	})
}

get(root + '/posts').then(
	function fulfilled(posts) {
		posts = JSON.parse(posts);
		console.log('First post is: "' + posts[0].title + '"');
	},
	function rejected(error) {
		console.error(error);
	}
)