Copy let promiseToClean = new Promise ( function (resolve , reject) {
let isClean = true ;
if (isClean){
resolve ( 'Clean' );
return ; //resolve/reject don't stop execution
}
reject ( 'Not Clean' );
});
//when promise is resolved
promiseToCleanTheRoom .then ((fromResolve) => {
console .log ( 'the room is' + fromResolve);
}) .catch ( (fromReject) => {
console .log ( 'the room is' + fromReject);
}));
Copy function screenshotPromise (windowId) {
return new Promise ((resolve , reject) => {
chrome . tabs .captureVisibleTab (windowId , {quality : 10 } ,
(screenshotData) => {
console .log ( "Screenshot" , screenshotData);
resolve (screenshotData);
});
});
}
Copy var promise1 = Promise .resolve ( 3 );
var promise2 = 42 ;
var promise3 = new Promise ( function (resolve , reject) {
setTimeout (resolve , 100 , 'foo' );
});
Promise .all ([promise1 , promise2 , promise3]) .then ( function (values) {
console .log (values);
});
// expected output: Array [3, 42, "foo"]
Copy function f () {
return new Promise ((resolve , reject) => reject ( new Error ( "Err" )))
}
async function g () {
throw new Error ( "Err" )
}
try {
await f ()
await g ()
} catch (err) {
//both throw same errors
}
When you return something from a then() callback, it's a bit magic. If you return a value, the next then() is called with that value. However, if you return something promise-like, the next then() waits on it, and is only called when that promise settles (succeeds/fails).
Copy getJSON ( 'story.json' ) .then ( function (story) {
return getJSON ( story .chapterUrls[ 0 ]);
}) .then ( function (chapter1) {
console .log ( "Got chapter 1!" , chapter1);
})