let variable = new Promise(function(resolve, reject) {
// Successful
resolve(value);
// Fails
reject(value);
});
variable.then(function(result) {
// Statement
}, function(error) {
// Statement
});
variable.catch(function(error) {
// Statement
});
variable.finally(function() {
// Statement
});
Promise.all([variable1, variable2, variable3])
Promise.allSettled([variable1, variable2, variable3])
Promise.race([variable1, variable2, variable3])
Promise.any([variable1, variable2, variable3])
Promise.resolve(value)
Promise.reject(error)
const functionName1 = async () => {
// Returns a Promise
}
async function functionName2 () {
let variable = await functionName1();
}
// Without options, a get request is sent
let p = fetch(URL, options);
p.then((response) => {
// Object contains "status", "ok", "headers" properties
() => return response.json();
}).then((response) => {
// Object contains "text", "json", "blob", "formData", "arrayBuffer" properties
console.log(response);
}).catch((error) => {
console.log(error);
})
const functionName = async () => {
let response = await fetch(URL, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({}),
});
let result = await response.json();
console.log(result);
}
var = new XMLHttpRequest()
=> Initiate an xhr Objectvar.open("GET", "url", "boolean")
=> Make a get request, from where method is making request, if uou want async request or notvar.onprogress = function() {statement}
=> Statements executes on progress var.onload = function () {
if(this.status === statusCode) {
console.log(this.responseText)
}
}
var.send()
=> Send get requestvar.onreadystatechange = function () {
console.log(var.readyState);
}
var.open("POST", "url", "boolean")
var.getResponseHeader('Content-type', 'type')
=> Returns the string containing the text of a particular header's valuevar.send(parameters)
=> Send post request