Promises
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);
}));Promisify
function screenshotPromise(windowId) {
return new Promise((resolve, reject) => {
chrome.tabs.captureVisibleTab(windowId, {quality: 10},
(screenshotData) => {
console.log("Screenshot", screenshotData);
resolve(screenshotData);
});
});
}PROMISE ALL
Error Handling
RETURNING IN then
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).
Last updated