xxxxxxxxxx
async function abc()
{
Promise.resolve("hello world").then(function(value)
{
console.log(value);
}
xxxxxxxxxx
const myPromise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('foo');
}, 300);
});
myPromise
.then(handleResolvedA, handleRejectedA)
.then(handleResolvedB, handleRejectedB)
.then(handleResolvedC, handleRejectedC);
xxxxxxxxxx
JS
copy
let done = true
const isItDoneYet = new Promise((resolve, reject) => {
if (done) {
const workDone = 'Here is the thing I built'
resolve(workDone)
} else {
const why = 'Still working on something else'
reject(why)
}
})
xxxxxxxxxx
p.then(onFulfilled, onRejected);
p.then(function(value) {
// run
}, function(reason) {
// fail
});
xxxxxxxxxx
new Promise((resolve, reject) => {
if (ok) { resolve(result) }
else { reject(error) }
})
xxxxxxxxxx
const count = true;
let countValue = new Promise(function (resolve, reject) {
if (count) {
resolve("There is a count value.");
} else {
reject("There is no count value");
}
});
console.log(countValue);
xxxxxxxxxx
async function abc()
{
const myPromise = new Promise(function(resolve, reject) {
resolve("hello world");
});
let y= await myPromise;
console.log(y);
/*"hello world*/
}
xxxxxxxxxx
let fetchSent = fetch("/test", {method:"POST", body: JSON.stringify({name:"NAME NAME NAME"}), headers: {'Content-type': 'application/json; charset=UTF-8'}});
const p1 = new Promise((resolve, reject) => {
resolve(fetchSent);
// or
// reject(new Error("Error!"));
})
return p1;
/*console.log(p1) will yield the fetch promise that was sent to you*/