xxxxxxxxxx
let promise = new Promise(function(resolve, reject){
//do something
});
xxxxxxxxxx
const anotherPromise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('this is the eventual value the promise will return');
}, 300);
});
// CONTINUATION
anotherPromise
.then(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
new Promise((resolve, reject) => {
if (ok) { resolve(result) }
else { reject(error) }
})
xxxxxxxxxx
async function abc()
{
Promise.resolve("hello world").then(function(value)
{
console.log(value);
}
xxxxxxxxxx
let myPromise = new Promise(function(myResolve, myReject) {
// "Producing Code" (May take some time)
myResolve(); // when successful
myReject(); // when error
});
// "Consuming Code" (Must wait for a fulfilled Promise)
myPromise.then(
function(value) { /* code if successful */ },
function(error) { /* code if some 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*/
}